Skip to content

Conversation

akar016012
Copy link

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.

This commit fixes issue openai#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
Copy link
Author

akar016012 commented Oct 1, 2025

@akar016012
Copy link
Author

Test cases for this PR:
image

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

Successfully merging this pull request may close these issues.

1 participant