feat: add support for reasoning field in chat completions #1092
+367
−10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
As reported in #1070 by @LubyRuffy, different OpenAI-compatible providers use different field names for reasoning content:
reasoning_contentreasoningThis PR provides transparent support for both formats without breaking existing implementations.
Changes
ChatCompletionMessage.UnmarshalJSONto support thereasoningfieldUnmarshalJSONmethod forChatCompletionStreamChoiceDeltareasoning_contentandreasoningfields map to the sameReasoningContentpropertyreasoning_contenttakes precedence overreasoning(backward compatible)Implementation Details
The implementation uses custom unmarshaling with a fallback mechanism:
reasoning_content(existing behavior)reasoningfieldReasoningContentfieldThis approach ensures:
Testing
Added comprehensive test coverage in
chat_reasoning_test.go:reasoning_content) supportreasoning) supportAll existing tests pass without modification.
Example Usage
// Works with both DeepSeek and LM Studio responses
stream, err := client.CreateChatCompletionStream(ctx, req)
for {
response, err := stream.Recv()
// ReasoningContent will be populated regardless of provider
reasoning := response.Choices[0].Delta.ReasoningContent
}## Breaking Changes
None. This is a fully backward-compatible enhancement.
Checklist