-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
json: cannot unmarshal object into Go struct field ChatCompletionResponseFormatJSONSchema.response_format.json_schema.schema of type json.Marshaler #884
Comments
I can confirm the bug. this is a minimal working example, taken from openai example: package main
import (
"encoding/json"
goopenai "github.com/sashabaranov/go-openai"
)
const request = `
{
"model": "gpt-4o-2024-08-06",
"stream": true,
"messages": [
{
"role": "system",
"content": "You are a helpful math tutor. Guide the user through the solution step by step."
},
{
"role": "user",
"content": "how can I solve 8x + 7 = -23"
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "math_reasoning",
"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
},
"strict": true
}
}
}
`
func main() {
ccr := &goopenai.ChatCompletionRequest{}
err := json.Unmarshal([]byte(request), ccr)
if err != nil {
panic(err)
}
} the issue here is that |
this is the offending MR I think. a3bd256#diff-c2ade2f386c41794d5ebc57ee49b57a5fca8082e03255e5bff13977cbc061287 |
I can also reproduce this @sashabaranov var req openai.ChatCompletionRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
log.Warn().Err(err).Msg("malformed JSON request")
return
} Request body is the very same example above, the helpful math tutor |
A temporary workaround: // Handle response format conversion if present
if req.ResponseFormat != nil {
openaiReq.ResponseFormat = &openai.ChatCompletionResponseFormat{
Type: openai.ChatCompletionResponseFormatType(req.ResponseFormat.Type),
}
if req.ResponseFormat.JSONSchema != nil {
openaiReq.ResponseFormat.JSONSchema = &openai.ChatCompletionResponseFormatJSONSchema{
Name: req.ResponseFormat.JSONSchema.Name,
Description: req.ResponseFormat.JSONSchema.Description,
Schema: req.ResponseFormat.JSONSchema.Schema,
Strict: req.ResponseFormat.JSONSchema.Strict,
}
}
} |
Is there any update on this? |
….Definition this will fix the issue sashabaranov#884 Signed-off-by: alejandrojnm <[email protected]>
I found if you pass the the object manually like JSON schema work , but if you use a Zod (from typescript) don't work, the object is parsed but when you send that to a custom endpoint (llama 3) don't work, llama respond with this |
json: cannot unmarshal object into Go struct field ChatCompletionResponseFormatJSONSchema.response_format.json_schema.schema of type json.Marshaler
github.com/sashabaranov/go-openai v1.32.3
The text was updated successfully, but these errors were encountered: