Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forced type conversion for static types #183

Open
qhenkart opened this issue Jan 24, 2025 · 4 comments
Open

forced type conversion for static types #183

qhenkart opened this issue Jan 24, 2025 · 4 comments

Comments

@qhenkart
Copy link

qhenkart commented Jan 24, 2025

I began to migrate to this SDK from the old SDK. For the most part everything is smooth minus a few verbosity things which I know are already planned on being removed.

I noticed a common design pattern in the tool calls
https://github.com/openai/openai-[go/blob/main/betathreadrunstep.go#L1264-L1274](https://www.golinks.io/blob/main/betathreadrunstep.go#L1264-L1274?trackSource=github)
structs contain anonymous interfaces with comments on which type to convert them to. Type conversion makes the code verbose and brittle. If the types are already known to the point where you write in the comments exactly what to convert them to. Why not just set them to the types as pointers so we can do a nil check instead of a conversion check?

Also incorrect documentation. Since you use the same type abstraction for both tool calls and message creation, it is incorrect to state that it is always "message_creation" it is in fact either "message_creation" or "tool_call". To match the API documentation you would need two different types, or keep the current pattern but update the comment
https://github.com/openai/openai-[go/blob/main/betathreadrunstep.go#L1332](https://www.golinks.io/blob/main/betathreadrunstep.go#L1332?trackSource=github)

Finally,
This one isn't clearly documented in the openAI SDK. The old SDK had it set to map[string]any. Here you guys allow for interface{}. To be clear, anything that is not a map[string]string{} will fail in the API. I made a similar issue in the old SDK as allowing for any type easily leads to API errors while following the SDK pattern
https://github.com/openai/openai-[go/blob/main/betathread.go#L289](https://www.golinks.io/blob/main/betathread.go#L289?trackSource=github)

I will continue to lead feedback as I migrate our application. I prefer to make PRs if you guys are open to accepting them from the community.

@jacobzim-stl
Copy link
Collaborator

Hi just to be clear, the new SDK design still hasn't been shipped.

I'm still interested in these issues you raise, but I'm afraid the links are broken.

@jacobzim-stl
Copy link
Collaborator

I think it was an issue with a github app, I'm checking the links now.

For the first issue you should be able to use the .AsUnion() method to switch into an interface containing the correct set of types. But I like your pointer idea as well, we'll consider that for discriminated unions in the next design.

Thanks for spotting the second issue. We'll get that fixed. The third issue also appears to be a bug.

We appreciate the feedback! We'll be opening a github discussions soon to review of the next design before it gets shipped. I'll be sure to echo your points there.

@qhenkart
Copy link
Author

qhenkart commented Jan 25, 2025

@jacobzim-stl I saw you guys changed the designation to Beta from Alpha so I thought the SDK was good to go.

As far as the .AsUnion goes. I see the abstraction now, and if the Function field (or FileSearch etc) were dynamically defined by the API, this abstraction would make total sense. However since both the API and the SDK define these fields as static, even if only in the comments by the SDK. A single item switch statement after calling AsUnion is even more verbose than a type conversion and equally as brittle.

Since the Function field can only ever have the runtime type of [FunctionToolCallFunction]. It would be much easier to work with if it was statically typed

I look forward to the upcoming updates of the SDK

@qhenkart
Copy link
Author

qhenkart commented Jan 25, 2025

I know you guys are removing the openai.F model. But I hope you caught that the API only accepts string values in the metadata and therefore metadata should be map[string]string otherwise an API error will be returned

Sharing the unnecessary complexity required to update the metadata of a thread

md := map[string]string{
  "success":           strconv.FormatBool(ar.Success),
}

 client.Beta.Threads.Update(ctx, threadID, newai.BetaThreadUpdateParams{
		Metadata: newai.F[interface{}](md),
}

also just ran into the embedding union. I assume there's a cleaner way to do this?

in := openai.EmbeddingNewParams{Input:  
openai.F[openai.EmbeddingNewParamsInputUnion(openai.EmbeddingNewParamsInputArrayOfStrings([]string{text})),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants