openai-go
openai-go copied to clipboard
fix: resolve union decoder field type reflection for param.Opt types
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.
Alternate solution PR: https://github.com/openai/openai-go/pull/527
Test cases for this PR: