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

Zod validation error is throwing when parsing error response

Open mbektimirov opened this issue 6 months ago • 4 comments

The schema from https://github.com/OpenRouterTeam/ai-sdk-provider/blob/main/src/openrouter-error.ts#L9 defines error code as a string, while it could be a number also (suddenly?). Additional error is combined from choices being undefined for the response which is still strictly defined as always present.

Just got this error for my file attachment, the model is openai/gpt-4.1:

{
  "name": "AI_TypeValidationError",
  "cause": {
    "issues": [
      {
        "code": "invalid_union",
        "unionErrors": [
          {
            "issues": [
              {
                "code": "invalid_type",
                "expected": "array",
                "received": "undefined",
                "path": [
                  "choices"
                ],
                "message": "Required"
              }
            ],
            "name": "ZodError"
          },
          {
            "issues": [
              {
                "code": "invalid_type",
                "expected": "string",
                "received": "undefined",
                "path": [
                  "error",
                  "type"
                ],
                "message": "Required"
              },
              {
                "code": "invalid_type",
                "expected": "string",
                "received": "number",
                "path": [
                  "error",
                  "code"
                ],
                "message": "Expected string, received number"
              }
            ],
            "name": "ZodError"
          }
        ],
        "path": [],
        "message": "Invalid input"
      }
    ],
    "name": "ZodError"
  },
  "value": {
    "error": {
      "message": "Provider returned error",
      "code": 400,
      "metadata": {
        "raw": "{\n  \"error\": {\n    \"code\": 400,\n    \"message\": \"The document has no pages.\",\n    \"status\": \"INVALID_ARGUMENT\"\n  }\n}\n",
        "provider_name": "Google"
      }
    },
    "user_id": "***"
  }
}

mbektimirov avatar Jun 12 '25 09:06 mbektimirov

This part is not supposed to be here also since the model I used was gpt-4.1, and I can also confirm no Google AI calls were made from my Activity page:

 "provider_name": "Google"

mbektimirov avatar Jun 12 '25 09:06 mbektimirov

Should be handled in #84

@mbektimirov can you check if you have a default model set in your settings: https://openrouter.ai/settings/preferences

louisgv avatar Jun 13 '25 01:06 louisgv

@louisgv It's set to Auto Router

Image

mbektimirov avatar Jun 13 '25 10:06 mbektimirov

I believe there is still an issue with the error schema. Hopefully this PR makes sense: https://github.com/OpenRouterTeam/ai-sdk-provider/pull/108

niekert avatar Jul 14 '25 18:07 niekert

Closing as resolved in v1.4.0

subtleGradient avatar Dec 06 '25 09:12 subtleGradient