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

mistral model does not works with SDK v5

Open omarkhatibco opened this issue 4 months ago • 2 comments

Hello Everyone,

I'm in the middle of the process to upgrade to AI SDK v5.

I'm trying to migrate a call to Mistral to use OCR. which was working in v4.

here is the code

 const result = await generateObject({
    model: openrouter.languageModel('mistralai/mistral-small-3.2-24b-instruct'),
    output: 'object',
    schema: z.object({
      text: z.string().describe('The extracted text from the file'),
    }),
    messages: [
      {
        role: 'user',
        content: [
          {
            type: 'text',
            text: 'Extract the all the content from the file',
          },
          {
            type: 'file',
            data: new URL(
              'https://github.com/vercel/ai/blob/main/examples/ai-core/data/ai.pdf?raw=true',
            ),
            mediaType: 'application/pdf',
            filename: 'ai.pdf',
          },
        ],
      },
    ],
    providerOptions: {
      mistral: {},
    },
  })

I tried both generateObject and generateText, I'm getting the same error Invalid JSON response

after some digging, I was able to extract the zod error.

[
  {
    "code": "invalid_value",
    "values": [
      "url_citation"
    ],
    "path": [
      "choices",
      0,
      "message",
      "annotations",
      0,
      "type"
    ],
    "message": "Invalid input: expected \"url_citation\""
  },
  {
    "expected": "object",
    "code": "invalid_type",
    "path": [
      "choices",
      0,
      "message",
      "annotations",
      0,
      "url_citation"
    ],
    "message": "Invalid input: expected object, received undefined"
  }
]

it seems either the schema is not correct, or the api does not return the data anymore.

omarkhatibco avatar Aug 14 '25 14:08 omarkhatibco

I've got same problem when using streamText() and configure OpenRouter file-parser engine to pdf-text or mistral-ocr.

The problem happens because OpenRouter return file annotations (as part of a cost optimization feature) back in the response or stream chunks. This breaks the ai-sdk v5's Zod schema validation. https://openrouter.ai/docs/features/multimodal/pdfs#skip-parsing-costs

Example Error:

{
  "name": "AI_TypeValidationError",
  "cause": [
    {
      "code": "invalid_union",
      "errors": [
        [
          {
            "code": "invalid_value",
            "values": [
              "url_citation"
            ],
            "path": [
              "choices",
              0,
              "delta",
              "annotations",
              5,
              "type"
            ],
            "message": "Invalid input: expected \"url_citation\""
          },
          {
            "expected": "object",
            "code": "invalid_type",
            "path": [
              "choices",
              0,
              "delta",
              "annotations",
              5,
              "url_citation"
            ],
            "message": "Invalid input: expected object, received undefined"
          }
        ],
        [
          {
            "expected": "object",
            "code": "invalid_type",
            "path": [
              "error"
            ],
            "message": "Invalid input: expected object, received undefined"
          }
        ]
      ],
      "path": [],
      "message": "Invalid input"
    }
  ],
  "value": {

Planxnx avatar Aug 28 '25 14:08 Planxnx

Bump on this!!

HashimMufti avatar Oct 29 '25 14:10 HashimMufti

Closing as resolved in v1.4.0 Working example: https://github.com/OpenRouterTeam/openrouter-examples/tree/main/typescript/ai-sdk-v5/src/plugin-file-parser

subtleGradient avatar Dec 06 '25 09:12 subtleGradient