7
7
"errors"
8
8
"io"
9
9
"net/http"
10
+ "reflect"
10
11
11
12
openai "github.com/meguminnnnnnnnn/go-openai/internal"
12
13
@@ -136,6 +137,8 @@ type ChatCompletionMessage struct {
136
137
137
138
// For Role=tool prompts this should be set to the ID given in the assistant's prior request to call a tool.
138
139
ToolCallID string `json:"tool_call_id,omitempty"`
140
+
141
+ ExtraFields map [string ]json.RawMessage `json:"-"`
139
142
}
140
143
141
144
func (m ChatCompletionMessage ) MarshalJSON () ([]byte , error ) {
@@ -144,29 +147,31 @@ func (m ChatCompletionMessage) MarshalJSON() ([]byte, error) {
144
147
}
145
148
if len (m .MultiContent ) > 0 {
146
149
msg := struct {
147
- Role string `json:"role"`
148
- Content string `json:"-"`
149
- Refusal string `json:"refusal,omitempty"`
150
- MultiContent []ChatMessagePart `json:"content,omitempty"`
151
- Name string `json:"name,omitempty"`
152
- ReasoningContent string `json:"reasoning_content,omitempty"`
153
- FunctionCall * FunctionCall `json:"function_call,omitempty"`
154
- ToolCalls []ToolCall `json:"tool_calls,omitempty"`
155
- ToolCallID string `json:"tool_call_id,omitempty"`
150
+ Role string `json:"role"`
151
+ Content string `json:"-"`
152
+ Refusal string `json:"refusal,omitempty"`
153
+ MultiContent []ChatMessagePart `json:"content,omitempty"`
154
+ Name string `json:"name,omitempty"`
155
+ ReasoningContent string `json:"reasoning_content,omitempty"`
156
+ FunctionCall * FunctionCall `json:"function_call,omitempty"`
157
+ ToolCalls []ToolCall `json:"tool_calls,omitempty"`
158
+ ToolCallID string `json:"tool_call_id,omitempty"`
159
+ ExtraFields map [string ]json.RawMessage `json:"-"`
156
160
}(m )
157
161
return json .Marshal (msg )
158
162
}
159
163
160
164
msg := struct {
161
- Role string `json:"role"`
162
- Content string `json:"content,omitempty"`
163
- Refusal string `json:"refusal,omitempty"`
164
- MultiContent []ChatMessagePart `json:"-"`
165
- Name string `json:"name,omitempty"`
166
- ReasoningContent string `json:"reasoning_content,omitempty"`
167
- FunctionCall * FunctionCall `json:"function_call,omitempty"`
168
- ToolCalls []ToolCall `json:"tool_calls,omitempty"`
169
- ToolCallID string `json:"tool_call_id,omitempty"`
165
+ Role string `json:"role"`
166
+ Content string `json:"content,omitempty"`
167
+ Refusal string `json:"refusal,omitempty"`
168
+ MultiContent []ChatMessagePart `json:"-"`
169
+ Name string `json:"name,omitempty"`
170
+ ReasoningContent string `json:"reasoning_content,omitempty"`
171
+ FunctionCall * FunctionCall `json:"function_call,omitempty"`
172
+ ToolCalls []ToolCall `json:"tool_calls,omitempty"`
173
+ ToolCallID string `json:"tool_call_id,omitempty"`
174
+ ExtraFields map [string ]json.RawMessage `json:"-"`
170
175
}(m )
171
176
return json .Marshal (msg )
172
177
}
@@ -177,32 +182,48 @@ func (m *ChatCompletionMessage) UnmarshalJSON(bs []byte) error {
177
182
Content string `json:"content"`
178
183
Refusal string `json:"refusal,omitempty"`
179
184
MultiContent []ChatMessagePart
180
- Name string `json:"name,omitempty"`
181
- ReasoningContent string `json:"reasoning_content,omitempty"`
182
- FunctionCall * FunctionCall `json:"function_call,omitempty"`
183
- ToolCalls []ToolCall `json:"tool_calls,omitempty"`
184
- ToolCallID string `json:"tool_call_id,omitempty"`
185
+ Name string `json:"name,omitempty"`
186
+ ReasoningContent string `json:"reasoning_content,omitempty"`
187
+ FunctionCall * FunctionCall `json:"function_call,omitempty"`
188
+ ToolCalls []ToolCall `json:"tool_calls,omitempty"`
189
+ ToolCallID string `json:"tool_call_id,omitempty"`
190
+ ExtraFields map [string ]json.RawMessage `json:"-"`
185
191
}{}
186
192
187
193
if err := json .Unmarshal (bs , & msg ); err == nil {
188
194
* m = ChatCompletionMessage (msg )
195
+ extra , err := openai .UnmarshalExtraFields (reflect .TypeOf (m ), bs )
196
+ if err != nil {
197
+ return err
198
+ }
199
+
200
+ m .ExtraFields = extra
189
201
return nil
190
202
}
203
+
191
204
multiMsg := struct {
192
205
Role string `json:"role"`
193
206
Content string
194
- Refusal string `json:"refusal,omitempty"`
195
- MultiContent []ChatMessagePart `json:"content"`
196
- Name string `json:"name,omitempty"`
197
- ReasoningContent string `json:"reasoning_content,omitempty"`
198
- FunctionCall * FunctionCall `json:"function_call,omitempty"`
199
- ToolCalls []ToolCall `json:"tool_calls,omitempty"`
200
- ToolCallID string `json:"tool_call_id,omitempty"`
207
+ Refusal string `json:"refusal,omitempty"`
208
+ MultiContent []ChatMessagePart `json:"content"`
209
+ Name string `json:"name,omitempty"`
210
+ ReasoningContent string `json:"reasoning_content,omitempty"`
211
+ FunctionCall * FunctionCall `json:"function_call,omitempty"`
212
+ ToolCalls []ToolCall `json:"tool_calls,omitempty"`
213
+ ToolCallID string `json:"tool_call_id,omitempty"`
214
+ ExtraFields map [string ]json.RawMessage `json:"-"`
201
215
}{}
202
216
if err := json .Unmarshal (bs , & multiMsg ); err != nil {
203
217
return err
204
218
}
205
219
* m = ChatCompletionMessage (multiMsg )
220
+
221
+ extra , err := openai .UnmarshalExtraFields (reflect .TypeOf (m ), bs )
222
+ if err != nil {
223
+ return err
224
+ }
225
+
226
+ m .ExtraFields = extra
206
227
return nil
207
228
}
208
229
0 commit comments