ai-sdk-provider icon indicating copy to clipboard operation
ai-sdk-provider copied to clipboard

json_schema is not supported

Open afonsomatos opened this issue 9 months ago • 4 comments

I'm trying to use this provider with models that offer structured output through json_schema. But with generateObject, it ALWAYS uses json_object, instead of json_schema.

{
  "model": "(any model)",
  "temperature": 0,
  "response_format": {
    "type": "json_object"
  },
  "messages": [
    {
      "role": "system",
      "content": "JSON schema:\n{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"}},\"required\":[\"name\"],\"additionalProperties\":false,\"$schema\":\"http://json-schema.org/draft-07/schema#\"}\nYou MUST answer with a JSON object that matches the JSON schema above."
    },
    {
      "role": "user",
      "content": "Hello, what's your name?"
    }
  ]
}

afonsomatos avatar Mar 06 '25 03:03 afonsomatos

i managed to use structed output with extrabody on the creaateOpenRouter function and passing the reponse_format as indicated on openrouter api. I have setupped mine in this way;

const openrouter = createOpenRouter({
  apiKey: "your-api-key",
  extraBody: {
    "X-Title": "Translator", // This is only to recognise this application on openrouter activity view
    response_format: {
      type: 'json_schema',
      json_schema: {
        name: 'translated',
        strict: true,
        schema: {
          type: 'array',
          items: {
            type: 'string',
            description: 'Translated paragraph'
          }   
        },
      }
    }
  }
})```

RobertoFretel avatar May 08 '25 16:05 RobertoFretel

did you use generateObject or generateText with this?

maxschulmeister avatar May 29 '25 16:05 maxschulmeister

I had some issues with structured output as well, but at least for some models, like Mistral small 3.2, what worked for me was to add mode: json:

streamObject({
      mode: "json",
      ...
});

Apparently, the default mode, always caused some issues for me. I think it should work for generateObject as well.

50BytesOfJohn avatar Jun 24 '25 14:06 50BytesOfJohn

we added support for this in https://github.com/theopenco/llmgateway-ai-sdk-provider via https://github.com/theopenco/llmgateway-ai-sdk-provider/pull/7

steebchen avatar Oct 13 '25 16:10 steebchen