Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions provider/openaiprovider/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (a *chatClient) run(ctx context.Context, messages []*message.Message, optio
if choice.Message.Refusal != "" {
contents = append(contents, &message.ErrorContent{Message: choice.Message.Refusal, ErrorCode: "Refusal"})
}
finishReason = choice.FinishReason
finishReason = normalizeChatFinishReason(choice.FinishReason)
}
if resp.JSON.Usage.Valid() {
contents = addUsage(contents, resp.Usage)
Expand Down Expand Up @@ -209,7 +209,7 @@ func (a *chatClient) run(ctx context.Context, messages []*message.Message, optio
}
var finishReason string
if len(chunk.Choices) > 0 {
finishReason = chunk.Choices[0].FinishReason
finishReason = normalizeChatFinishReason(chunk.Choices[0].FinishReason)
}
resp := &agent.ResponseUpdate{
Contents: contents,
Expand All @@ -230,6 +230,17 @@ func (a *chatClient) run(ctx context.Context, messages []*message.Message, optio
}
}

// normalizeChatFinishReason maps the deprecated "function_call" finish reason,
// still emitted by some OpenAI-compatible endpoints, onto the canonical
// "tool_calls", matching the Python client. Other reasons pass through
// unchanged.
func normalizeChatFinishReason(reason string) string {
if reason == "function_call" {
return "tool_calls"
}
return reason
}

func mapRole(r string) message.Role {
switch r {
case "user":
Expand Down
19 changes: 19 additions & 0 deletions provider/openaiprovider/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,25 @@ func TestChatBasicRequestResponse_NonStreaming(t *testing.T) {
}
}

// Some OpenAI-compatible endpoints still emit the deprecated "function_call"
// finish reason; it must be normalized to the canonical "tool_calls", matching
// the Python client.
func TestChatLegacyFunctionCallFinishReasonNormalized(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{"id":"chatcmpl-fc","object":"chat.completion","created":1727888631,"model":"gpt-4o-mini","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"function_call"}]}`)
}))
defer server.Close()

resp, err := newTestClient(server).RunText(t.Context(), "hi").Collect()
if err != nil {
t.Fatalf("error = %v", err)
}
if resp.FinishReason != "tool_calls" {
t.Errorf("FinishReason = %q, want %q", resp.FinishReason, "tool_calls")
}
}

func TestChatURLCitationAnnotations_NonStreaming(t *testing.T) {
const input = `
{
Expand Down
Loading