Skip to content

Commit fc6aaa0

Browse files
committed
Add support for reasoning_content field in chat completion messages for DeepSeek R1
1 parent be51738 commit fc6aaa0

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

chat.go

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ type ChatMessagePart struct {
9292
}
9393

9494
type ChatCompletionMessage struct {
95-
Role string `json:"role"`
96-
Content string `json:"content,omitempty"`
97-
Refusal string `json:"refusal,omitempty"`
98-
MultiContent []ChatMessagePart
95+
Role string `json:"role"`
96+
ReasoningContent string `json:"reasoning_content,omitempty"`
97+
Content string `json:"content"`
98+
Refusal string `json:"refusal,omitempty"`
99+
MultiContent []ChatMessagePart
99100

100101
// This property isn't in the official documentation, but it's in
101102
// the documentation for the official library for python:
@@ -118,56 +119,60 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
118119
}
119120
if len(m.MultiContent) > 0 {
120121
msg := struct {
121-
Role string `json:"role"`
122-
Content string `json:"-"`
123-
Refusal string `json:"refusal,omitempty"`
124-
MultiContent []ChatMessagePart `json:"content,omitempty"`
125-
Name string `json:"name,omitempty"`
126-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
127-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
128-
ToolCallID string `json:"tool_call_id,omitempty"`
122+
Role string `json:"role"`
123+
ReasoningContent string `json:"reasoning_content,omitempty"`
124+
Content string `json:"-"`
125+
Refusal string `json:"refusal,omitempty"`
126+
MultiContent []ChatMessagePart `json:"content,omitempty"`
127+
Name string `json:"name,omitempty"`
128+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
129+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
130+
ToolCallID string `json:"tool_call_id,omitempty"`
129131
}(m)
130132
return json.Marshal(msg)
131133
}
132134

133135
msg := struct {
134-
Role string `json:"role"`
135-
Content string `json:"content,omitempty"`
136-
Refusal string `json:"refusal,omitempty"`
137-
MultiContent []ChatMessagePart `json:"-"`
138-
Name string `json:"name,omitempty"`
139-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
140-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
141-
ToolCallID string `json:"tool_call_id,omitempty"`
136+
Role string `json:"role"`
137+
ReasoningContent string `json:"reasoning_content,omitempty"`
138+
Content string `json:"content"`
139+
Refusal string `json:"refusal,omitempty"`
140+
MultiContent []ChatMessagePart `json:"-"`
141+
Name string `json:"name,omitempty"`
142+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
143+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
144+
ToolCallID string `json:"tool_call_id,omitempty"`
142145
}(m)
143146
return json.Marshal(msg)
144147
}
145148

146149
func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error {
147150
msg := struct {
148-
Role string `json:"role"`
149-
Content string `json:"content,omitempty"`
150-
Refusal string `json:"refusal,omitempty"`
151-
MultiContent []ChatMessagePart
152-
Name string `json:"name,omitempty"`
153-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
154-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
155-
ToolCallID string `json:"tool_call_id,omitempty"`
151+
Role string `json:"role"`
152+
ReasoningContent string `json:"reasoning_content,omitempty"`
153+
Content string `json:"content"`
154+
Refusal string `json:"refusal,omitempty"`
155+
MultiContent []ChatMessagePart
156+
Name string `json:"name,omitempty"`
157+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
158+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
159+
ToolCallID string `json:"tool_call_id,omitempty"`
156160
}{}
157161

158162
if err := json.Unmarshal(bs, &msg); err == nil {
159163
*m = ChatCompletionMessage(msg)
160164
return nil
161165
}
162166
multiMsg := struct {
163-
Role string `json:"role"`
164-
Content string
165-
Refusal string `json:"refusal,omitempty"`
166-
MultiContent []ChatMessagePart `json:"content"`
167-
Name string `json:"name,omitempty"`
168-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
169-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
170-
ToolCallID string `json:"tool_call_id,omitempty"`
167+
Role string `json:"role"`
168+
ReasoningContent string `json:"reasoning_content,omitempty"`
169+
Content string
170+
Refusal string `json:"refusal,omitempty"`
171+
MultiContent []ChatMessagePart `json:"content"`
172+
Name string `json:"name,omitempty"`
173+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
174+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
175+
ToolCallID string `json:"tool_call_id,omitempty"`
171176
}{}
172177
if err := json.Unmarshal(bs, &multiMsg); err != nil {
173178
return err

chat_stream.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
)
77

88
type ChatCompletionStreamChoiceDelta struct {
9-
Content string `json:"content,omitempty"`
10-
Role string `json:"role,omitempty"`
11-
FunctionCall *FunctionCall `json:"function_call,omitempty"`
12-
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
13-
Refusal string `json:"refusal,omitempty"`
9+
Content string `json:"content,omitempty"`
10+
ReasoningContent string `json:"reasoning_content,omitempty"`
11+
Role string `json:"role,omitempty"`
12+
FunctionCall *FunctionCall `json:"function_call,omitempty"`
13+
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
14+
Refusal string `json:"refusal,omitempty"`
1415
}
1516

1617
type ChatCompletionStreamChoiceLogprobs struct {

chat_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,9 @@ func TestO1ModelChatCompletions(t *testing.T) {
300300
MaxCompletionTokens: 1000,
301301
Messages: []openai.ChatCompletionMessage{
302302
{
303-
Role: openai.ChatMessageRoleUser,
304-
Content: "Hello!",
303+
Role: openai.ChatMessageRoleUser,
304+
Content: "Hello!",
305+
ReasoningContent: "hi!",
305306
},
306307
},
307308
})

0 commit comments

Comments
 (0)