Skip to content

missing discriminator registration for ChatCompletionToolChoiceOptionUnionParam #520

@outlore-dev

Description

@outlore-dev

unmarshalling into ChatCompletionToolChoiceOptionUnionParam does not work

openai-go version: v2.6.1
go 1.25

Reproduction:

import (
	"encoding/json"
	"testing"

	"github.com/openai/openai-[go/v2](https://www.golinks.io/v2?trackSource=github)"
	"github.com/openai/openai-[go/v2/packages/param](https://www.golinks.io/v2/packages/param?trackSource=github)"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestBugRepro(t *testing.T) {
	t.Run("test_bug_repro", func(t *testing.T) {
		jsonStr := `{
			"model": "gpt-4",
			"messages": [{"role": "user", "content": "test"}],
			"tool_choice": {"type": "function", "function": {"name": "my_tool"}}
		}`

		var params openai.ChatCompletionNewParams
		err := json.Unmarshal([]byte(jsonStr), &params)
		require.NoError(t, err)

		tc := params.ToolChoice

		// ❌ BUG: This assertion FAILS (but shouldn't)
		assert.True(t, !param.IsOmitted(tc.OfFunctionToolChoice),
			"OfFunctionToolChoice should be set for function tool choice")

		// ❌ BUG: This assertion PASSES (but shouldn't)
		assert.False(t, !param.IsOmitted(tc.OfAllowedTools),
			"OfAllowedTools should NOT be set for function tool choice")

		if !param.IsOmitted(tc.OfAllowedTools) {
			// The function name is lost
			marshaled, _ := json.Marshal(tc)
			t.Logf("   Original JSON had: {\"type\": \"function\", \"function\": {\"name\": \"my_tool\"}}")
			t.Logf("   After parse+marshal: %s", string(marshaled))
			t.Logf("   ⚠️ Function name 'my_tool' is lost!")
		}
	})
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions