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

missing discriminator registration for ChatCompletionToolChoiceOptionUnionParam

Open outlore-dev opened this issue 2 months ago • 2 comments

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!")
		}
	})
}

outlore-dev avatar Sep 23 '25 03:09 outlore-dev

@outlore-dev I tested and it is indeed an issue, please take a look at the PR.

akar016012 avatar Oct 01 '25 03:10 akar016012

After the hopeful fix: Image

akar016012 avatar Oct 01 '25 03:10 akar016012