-
Notifications
You must be signed in to change notification settings - Fork 238
Description
Description
The ChatCompletionChunkChoiceDelta struct's Role field lacks the omitempty JSON tag, causing zero-value empty strings to be serialized when re-marshaling streaming chunks. This violates the OpenAI API specification and breaks compatibility with strict OpenAI-compatible clients.
The OpenAI OpenAPI spec looks correct, but the OpenAI Platform state that the chat completions' choices.delta.role field is string when it should be string or null. This has also been highlighted in openai-openapi/issues/504. I suspect it's not strictly a Go SDK issue but rather a tooling configuration (Stainless?) issue, as the SDK and its types are generated.
Current Behavior
Struct definition from chatcompletion.go:
// A chat completion delta generated by streamed model responses.
type ChatCompletionChunkChoiceDelta struct {
// ...
// The refusal message generated by the model.
Refusal string `json:"refusal,nullable"`
// The role of the author of this message.
//
// Any of "developer", "system", "user", "assistant", "tool".
Role string `json:"role"` // missing omitempty!
ToolCalls []ChatCompletionChunkChoiceDeltaToolCall `json:"tool_calls"`
// ...
}When a ChatCompletionChunk with zero-value Role field is marshaled to JSON, it produces:
{
"choices": [{
"delta": {
"role": "", // Empty string should be omitted
"content": "..."
}
}]
}Expected Behavior
Making a request to OpenAI endpoints demonstrate the expected behavior and type definitions. The request
curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": "Say hello"}],
"stream": true,
"max_tokens": 10
}'gets the response:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1763465300,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"xxx","choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}],"obfuscation":"xxx"}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1763465300,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"xxx","choices":[{"index":0,"delta":{"content":"Hello"},"logprobs":null,"finish_reason":null}],"obfuscation":"xxx"}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1763465300,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"xxx","choices":[{"index":0,"delta":{"content":"!"},"logprobs":null,"finish_reason":null}],"obfuscation":"xxx"}
... 6 chunks ...
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1763465300,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"xxx","choices":[{"index":0,"delta":{"content":"?"},"logprobs":null,"finish_reason":null}],"obfuscation":"xxx"}
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","created":1763465300,"model":"gpt-4o-mini-2024-07-18","service_tier":"default","system_fingerprint":"xxx","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}],"obfuscation":"xxx"}
data: [DONE]
As we can see, the first chunk includes role: "assistant" while the subsequent chunks omit the field entirely, and not as an empty string.