OpenRouterTeam / ai-sdk-provider

The OpenRouter provider for the Vercel AI SDK contains support for hundreds of models through the OpenRouter chat and completion APIs.
https://www.npmjs.com/package/@openrouter/ai-sdk-provider
Apache License 2.0
316 stars 55 forks source link

Zod validation error is throwing when parsing error response #82

Open mbektimirov opened 1 month ago

mbektimirov commented 1 month ago

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 commented 1 month ago

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"
louisgv commented 1 month ago

Should be handled in #84

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

mbektimirov commented 1 month ago

@louisgv It's set to Auto Router

Image

niekert commented 3 days ago

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