-
Notifications
You must be signed in to change notification settings - Fork 215
Open
Description
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), ¶ms)
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
Labels
No labels