Skip to content

Cannot bind response_format.json_schema.schema into json.Marshaler field for structured outputs in chat/completions #1027

@AyushSawant18588

Description

@AyushSawant18588

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:

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

  1. 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.

  2. Else, Can we update the ChatCompletionResponseFormatJSONSchema struct (in configuration or code) so that this bind is successful?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions