Skip to content
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

Open
nodelrd opened this issue Oct 22, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@nodelrd
Copy link

nodelrd commented Oct 22, 2024

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

@nodelrd nodelrd added the bug Something isn't working label Oct 22, 2024
@lei-lei-shanda
Copy link

lei-lei-shanda commented Nov 6, 2024

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 json.Marshaler is an interface. It would be better if this is jsonschema.Definition or json.RawMessage.

@lei-lei-shanda
Copy link

@lukeocodes
Copy link

lukeocodes commented Jan 8, 2025

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

@lukeocodes
Copy link

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,
			}
		}
	}

@alejandrojnm
Copy link

Is there any update on this?

alejandrojnm added a commit to alejandrojnm/go-openai that referenced this issue Jan 24, 2025
….Definition this will fix the issue sashabaranov#884

Signed-off-by: alejandrojnm <[email protected]>
@alejandrojnm
Copy link

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 error, status code: 500, status: 500 Internal Server Error, message: invalid character 'I' looking for beginning of value, body: Internal Server Error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants