-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
I’m building a service-to-service integration that receives Chat Completion requests and binds them directly into the Go client’s ChatCompletionRequest struct using Gin:
var req openai.ChatCompletionRequest
if err := c.ShouldBindJSON(&req); err != nil {
// handle binding error
}
The struct for the JSON Schema portion is:
Line 220 in c125ae2
Schema json.Marshaler `json:"schema"` |
Because json.Marshaler only implements MarshalJSON() and not UnmarshalJSON(), any nested object under "schema" fails to bind.
Reproduction
Send an HTTP POST with a body like:
{
"model": "llama3-1b",
"messages": [
{ "role": "system", "content": "You are a helpful math tutor." },
{ "role": "user", "content": "solve 8x + 31 = 2" }
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "math_response",
"strict": true,
"schema": {
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {
"type": "object",
"properties": {
"explanation": { "type": "string" },
"output": { "type": "string" }
},
"required": ["explanation","output"],
"additionalProperties": false
}
},
"final_answer": { "type": "string" }
},
"required": ["steps","final_answer"],
"additionalProperties": false
}
}
}
}
Actual error:
json: cannot unmarshal object into Go struct field
ChatCompletionResponseFormatJSONSchema.response_format.json_schema.schema of type json.Marshaler
Expected Behavior:
The request should bind successfully into openai.ChatCompletionRequest without modifying the client library.
I can include a full JSON Schema object under "schema" and have it round-trip through Gin’s ShouldBindJSON into the existing json.Marshaler field.
Question
-
Is there a valid JSON payload I can send right now—without changing the client’s struct definition—that will satisfy ShouldBindJSON and allow my handler to bind into ChatCompletionResponseFormatJSONSchema.Schema? If so, please provide an example.
-
Else, Can we update the ChatCompletionResponseFormatJSONSchema struct (in configuration or code) so that this bind is successful?
Thank you!