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

fix: resolve union decoder field type reflection for param.Opt types

Open akar016012 opened this issue 2 months ago • 2 comments

This commit fixes issue #520 by improving the union decoder's ability to handle param.Opt types during JSON unmarshaling. The problem occurred when the decoder tried to call .Elem() on param.Opt[string] types, which are not pointer types.

Modified files and changes:

  • chatcompletion.go:

    • Added init() function to register ChatCompletionToolChoiceOptionUnionParam with apijson
    • Registered discriminators for "allowed_tools", "function", and "custom" types
    • Enables proper union unmarshaling through the apijson framework
  • internal/apijson/union.go:

    • Fixed import ordering (moved param import after reflect)
    • Enhanced newStructUnionDecoder to properly handle non-pointer field types
    • Added fieldType variable to check if field is a pointer before calling .Elem()
    • Only calls .Elem() on pointer types, preventing runtime panics

This fix ensures that union parameters can be properly decoded without type reflection errors, specifically addressing cases where union fields use param.Opt types instead of pointer types.

akar016012 avatar Oct 01 '25 04:10 akar016012

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

akar016012 avatar Oct 01 '25 04:10 akar016012

Test cases for this PR: image

akar016012 avatar Oct 01 '25 05:10 akar016012