openai-go icon indicating copy to clipboard operation
openai-go copied to clipboard

fix: handle param.Opt types in ChatCompletionToolChoiceOptionUnionParam unmarshaling

Open akar016012 opened this issue 2 months ago • 1 comments

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.

akar016012 avatar Oct 01 '25 03:10 akar016012

Alternate solution PR : https://github.com/openai/openai-go/pull/528

akar016012 avatar Oct 01 '25 04:10 akar016012