Skip to content

Getting an issue integrating a custom provider: AI_TypeValidationError, failed type validation #1298

Closed as not planned

Description

@bhaswata08
AI_TypeValidationError: Type validation failed: Value: {"id":"usage-1753417795","provider_id":"or","model":"qwen3-
coder","usage":{"prompt_tokens":5186,"completion_tokens":35,"total_tokens":5221,"multiplier_applied":2,"prompt_tokens_details":
{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":0,"audio_tokens":0,"accepted_prediction_to
kens":0,"rejected_prediction_tokens":0},"zj_usage":{"multiplier":2,"prompt_cost":10372,"completion_cost":70,"total_cost":10442,
"credits_remaining":282833.449999981}},"request_id":"zj-v1cc-fbf09f7e-8f1d-4d50-b7ae-a1f268cb2d48"}.
Error message:
[{"code":"invalid_union","errors":[[{"expected":"array","code":"invalid_type","path":["choices"],"message":"Invalid input:
expected array, received undefined"}],[{"expected":"object","code":"invalid_type","path":["error"],"message":"Invalid input:
expected object, received undefined"}]],"path":[],"message":"Invalid input"}]

opencode.json

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "zukijourney": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "https://api.zukijourney.com/v1"
      },
      "models": {
        "qwen3-coder": {
          "limit": {
            "context": 262000,
            "output": 2048
          }
        }
      }
    }
  }
}

Activity

  1. self-assigned this
    on Jul 25, 2025
  2. bhaswata08 commented on Aug 1, 2025

    @bhaswata08
    Author

    Any updates on this? @thdxr or please refer me to the file which causes this, i will fix it

  3. de-mux commented on Oct 17, 2025

    @de-mux

    Any updates on this? @thdxr or please refer me to the file which causes this, i will fix it

    For what it's worth, the AI_TypeValidationError appears to ultimately come from the vercel/ai dependency.

  4. rekram1-node commented on Oct 17, 2025

    @rekram1-node
    Collaborator

    The issue here seems to be the api not returning things in the correct format, so you need to either change the provider package or maybe use a different endpoint.

    @bhaswata08 is this still an issue for u?

  5. bhaswata08 commented on Nov 20, 2025

    @bhaswata08
    Author

    Sorry for the extremely late reply @rekram1-node, but yes, this is still an issue for me. This is primarily a provider issue since the provider uses non standard api reply, I would like to be pointed to the right resources to change this if possible.

    Thank you

  6. kaanyalti commented on Dec 3, 2025

    @kaanyalti

    Running into this as well

    AI_TypeValidationError: Type validation failed: Value: {"code":"Some resource has been exhausted","error":"Channel xcode-0828-joint-klfix-lap4-unified-qt85b-0 is overloaded: load=581520, num_requests=11"}.
    Error message: [{"code":"invalid_union","errors":[[{"expected":"array","code":"invalid_type","path":["choices"],"message":"Invalid input: expected array, received undefined"}],[{"expected":"object","code":"invalid_type","path":["error"],"message":"Invalid input: expected object, received string"}]],"path":[],"message":"Invalid input"}]
    

    Using grok

  7. rekram1-node commented on Dec 3, 2025

    @rekram1-node
    Collaborator

    @kaanyalti looks like that is a 529 from xai but sent as a stream messsage under a 200 status code, we have some logic to handle this can add grok case in too

  8. rekram1-node commented on Dec 3, 2025

    @rekram1-node
    Collaborator

    Added case for it:
    91db82c

  9. SuperSonnix71 commented on Dec 15, 2025

    @SuperSonnix71

    @rekram1-node I'm also getting this issue. Did some analysis and i think i know what is going on.

    Resource exhaustion errors return error objects instead of the expected OpenAI response structure with choices arrays, causing Zod validation to fail.
    The AI_TypeValidationError is coming from the AI SDK's validation layer and the OpenAI response schema expects a choices array, but error responses don't contain this field and in my case the root cause seems to be that when APIs are rate-limited or exhausted, they return error objects instead of the expected success response format.

    Note: i get this with the Grok model which is free. I switched to Big Pickle and the problem is gone

  10. Hcode00 commented on Dec 15, 2025

    @Hcode00

    @rekram1-node I'm also getting this issue. Did some analysis and i think i know what is going on.

    Resource exhaustion errors return error objects instead of the expected OpenAI response structure with choices arrays, causing Zod validation to fail. The AI_TypeValidationError is coming from the AI SDK's validation layer and the OpenAI response schema expects a choices array, but error responses don't contain this field and in my case the root cause seems to be that when APIs are rate-limited or exhausted, they return error objects instead of the expected success response format.

    Note: i get this with the Grok model which is free. I switched to Big Pickle and the problem is gone

    can confirm the exact same issue with Opencode Zen Grok Code Fast 1

  11. pwhack commented on Dec 18, 2025

    @pwhack

    I'm getting the same error trying to use Cortecs provider and model Devstral 2 2512.

    AI_TypeValidationError: Type validation failed: Value: {"status":500,"message":"Internal server error."}.
    Error message: [{"code":"invalid_union","errors":[[{"expected":"array","code":"invalid_type","path":["choices"],"message":"Invalid input: expected array, received undefined"}],[{"expected":"object","code":"invalid_type","path":["error"],"message":"Invalid input: expected object, received undefined"}]],"path":[],"message":"Invalid input"}]
    
  12. rekram1-node commented on Dec 18, 2025

    @rekram1-node
    Collaborator

    That's your provider sending a 500 response as a streamed message that isn't compliant with whatever format ur request goes through, most likely openai compat

  13. SuperSonnix71 commented on Dec 21, 2025

    @SuperSonnix71

    @rekram1-node I looked into commit 91db82c and I think I understand why we're still seeing this error.

    The problem is with the order of operations. Here's what's happening:

    When Grok's API is overloaded, it returns an error response like {"code": "Some resource has been exhausted"}. The Vercel AI SDK tries to validate this response and expects a choices array to be present. Since there's no choices array in the error response, Zod validation fails immediately and throws AI_TypeValidationError. The retry logic in retry.ts never gets a chance to run because the validation error is thrown before we get there.

    The commit added handling for the "Some resource has been exhausted" case in the retryable() function, which is great. But that function only gets called if we make it past the validation layer. Since the validation happens first and fails hard on malformed responses, the retry logic is never reached.

    I think the issue is that we need to either catch these responses before they hit the Zod validation, or make the validation schema flexible enough to accept both success and error response formats. Right now the validation is too strict and doesn't give the retry logic a chance to do its job. The error gets thrown at the validation layer and never makes it down to where the retry handling actually lives.

    A few ways this could be fixed:
    add a pre-validation step that checks if the response is an error object and handles it accordingly before schema validation runs, or update the Zod schema to accept both success responses with choices an error responses with error codes and route based on which one we got, or wrap the validation in a try-catch that specifically looks for validation errors caused by missing choices arrays and then checks if it's actually a retryable error response.

  14. labolado commented on Jan 14, 2026

    @labolado

    I've also encountered this problem very frequently.

    AI_TypeValidationError: Type validation failed: Value: {"error":"Stream error: error decoding response body"}. Error message: [ { "code": "invalid_union", "errors": [], "note": "No matching discriminator", "discriminator": "type", "path": [ "type" ], "message": "Invalid input" } ]

  15. PengMai1998 commented on Jan 16, 2026

    @PengMai1998

    I'm using the Antigravity Tools reverse proxy. I encounter this issue intermittently, and it causes the process to terminate immediately. Is it possible to implement an automatic retry mechanism when this happens?

    AI_TypeValidationError: Type validation failed: Value: {"error":"Stream error: error decoding response body"}.
    Error message: [
      {
        "code": "invalid_union",
        "errors": [],
        "note": "No matching discriminator",
        "discriminator": "type",
        "path": [
          "type"
        ],
        "message": "Invalid input"
      }
    ]
    
  16. Stargazer1209 commented on Jan 16, 2026

    @Stargazer1209

    When requesting stream response from aliyun( alibaba, alibaba(china), etc ) and some other providers, they return only first chunk with role: assistant while following chunks with empty role, which causes the schema validation fail all the time, resulting in a AI_TypeValidationError.
    I think we should add an extra option for custom providers to disable schema validation or allow for loose json structure, an option to disable stream request could also address this problem.
    Similarly, @ai-sdk/openai has an option named "strictJsonSchema" which controls if schema validation of the strict level is enforced.

  17. jaybyrrd commented on Mar 3, 2026

    @jaybyrrd

    Hi, my team is willing to work on this but want guidance on the preferred solution here. @thdxr, I saw you assigned this to yourself, is this something you can share guidance on?

  18. github-actions commented on May 3, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions