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
7 changes: 7 additions & 0 deletions provider/openaiprovider/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ func (a *chatClient) run(ctx context.Context, messages []*message.Message, optio
}
}
}
// Request usage in the final stream chunk. OpenAI omits the usage chunk for
// streamed completions unless stream_options.include_usage is set, so without
// this a streamed run reports zero token usage, unlike the non-streaming path.
// Respect an explicit caller value if one was already supplied.
if !body.StreamOptions.IncludeUsage.Valid() {
body.StreamOptions.IncludeUsage = openai.Bool(true)
}
return func(yield func(*agent.ResponseUpdate, error) bool) {
stream := a.client.Chat.Completions.NewStreaming(ctx, body, telemetryRequestOption)
defer func() { _ = stream.Close() }()
Expand Down
43 changes: 41 additions & 2 deletions provider/openaiprovider/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func TestChatBasicRequestResponse_Streaming(t *testing.T) {
"messages":[{"role":"user","content":"hello"}],
"model":"gpt-4o-mini",
"stream":true,
"stream_options":{"include_usage":true},
"max_completion_tokens":20
}
`
Expand Down Expand Up @@ -342,7 +343,8 @@ func TestChatStreamingRefusal_SurfacesErrorContent(t *testing.T) {
{
"messages":[{"role":"user","content":"do something disallowed"}],
"model":"gpt-4o-mini",
"stream":true
"stream":true,
"stream_options":{"include_usage":true}
}
`
const output = `data: {"id":"chatcmpl-refusal01","object":"chat.completion.chunk","created":1727889370,"model":"gpt-4o-mini-2024-07-18","choices":[{"index":0,"delta":{"role":"assistant","refusal":"I'm sorry, I can't "},"finish_reason":null}],"usage":null}
Expand Down Expand Up @@ -378,6 +380,41 @@ data: [DONE]
}
}

// TestChatStreamingIncludeUsage_RespectsCallerOverride verifies that when the
// caller explicitly sets stream_options.include_usage the provider does not
// override it with the default true value.
func TestChatStreamingIncludeUsage_RespectsCallerOverride(t *testing.T) {
const input = `
{
"messages":[{"role":"user","content":"hello"}],
"model":"gpt-4o-mini",
"stream":true,
"stream_options":{"include_usage":false}
}
`
const output = `data: {"id":"chatcmpl-1","object":"chat.completion.chunk","created":1727889370,"model":"gpt-4o-mini-2024-07-18","choices":[{"index":0,"delta":{"role":"assistant","content":"Hi"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-1","object":"chat.completion.chunk","created":1727889370,"model":"gpt-4o-mini-2024-07-18","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}

data: [DONE]

`
server := newTestServerStreaming(t, input, output)
defer server.Close()

a := newTestClient(server)

for _, err := range a.RunText(t.Context(), "hello", openaiprovider.ChatCompletionNewParams(openai.ChatCompletionNewParams{
StreamOptions: openai.ChatCompletionStreamOptionsParam{
IncludeUsage: openai.Bool(false),
},
}), agent.Stream(true)) {
if err != nil {
t.Fatalf("error = %v", err)
}
}
}

func TestChatMultipleMessages_NonStreaming(t *testing.T) {
const input = `
{
Expand Down Expand Up @@ -878,7 +915,8 @@ func TestChatFunctionCallContent_Streaming(t *testing.T) {
}
],
"model": "gpt-4o-mini",
"stream": true
"stream": true,
"stream_options": {"include_usage": true}
}
`
const output = `data: {"id":"chatcmpl-ADymNiWWeqCJqHNFXiI1QtRcLuXcl","object":"chat.completion.chunk","created":1727895263,"model":"gpt-4o-mini-2024-07-18","system_fingerprint":"fp_f85bea6784","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_F9ZaqPWo69u0urxAhVt8meDW","type":"function","function":{"name":"GetPersonAge","arguments":""}}],"refusal":null},"logprobs":null,"finish_reason":null}],"usage":null}
Expand Down Expand Up @@ -1178,6 +1216,7 @@ func TestChatOptions_Model_OverridesClientModel_Streaming(t *testing.T) {
"messages":[{"role":"user","content":"hello"}],
"model":"gpt-4o",
"stream":true,
"stream_options":{"include_usage":true},
"max_completion_tokens":20
}
`
Expand Down