openai-go
openai-go copied to clipboard
fix: handle param.Opt types in ChatCompletionToolChoiceOptionUnionParam unmarshaling
This commit addresses issue #520 where the JSON unmarshaling fails for ChatCompletionToolChoiceOptionUnionParam when encountering OfAuto field with param.Opt[string] type. The decoder.field.Type.Elem() method was calling .Elem() on param.Opt[string] which is not a pointer type, causing unmarshaling to fail.
Modified files and changes:
- chatcompletion.go:
- Added custom UnmarshalJSON method for ChatCompletionToolChoiceOptionUnionParam
- Implemented proper handling of simple string values for OfAuto field
- Added discriminated object unmarshaling based on "type" field
- Added support for "function", "allowed_tools", and "custom" types
- Replaced generic apijson.UnmarshalRoot call with type-specific handling
This fix ensures that the union parameter can properly unmarshal both string values and complex objects without encountering type reflection errors when dealing with param.Opt types.
Alternate solution PR : https://github.com/openai/openai-go/pull/528