You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Getting an issue integrating a custom provider: AI_TypeValidationError, failed type validation #1298
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.
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.
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"}]
@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
@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
@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
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
@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.
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?
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.
Reacted by Johan Sim Jian An, Zhonghao Guo, KangSol and Dmitrii Stepanidenko
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?
Activity
bhaswata08 commentedon Aug 1, 2025
Any updates on this? @thdxr or please refer me to the file which causes this, i will fix it
de-mux commentedon Oct 17, 2025
For what it's worth, the
AI_TypeValidationErrorappears to ultimately come from the vercel/ai dependency.rekram1-node commentedon Oct 17, 2025
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?
bhaswata08 commentedon Nov 20, 2025
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
kaanyalti commentedon Dec 3, 2025
Running into this as well
Using grok
rekram1-node commentedon Dec 3, 2025
@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
rekram1-node commentedon Dec 3, 2025
We have some special parsing for it:
https://github.com/sst/opencode/blob/0eb97086fce1283047fd2296fcdbc37b09b4763f/packages/opencode/src/session/retry.ts#L56-L69
rekram1-node commentedon Dec 3, 2025
Added case for it:
91db82c
SuperSonnix71 commentedon Dec 15, 2025
@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_TypeValidationErroris 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
Hcode00 commentedon Dec 15, 2025
can confirm the exact same issue with Opencode Zen Grok Code Fast 1
pwhack commentedon Dec 18, 2025
I'm getting the same error trying to use Cortecs provider and model Devstral 2 2512.
rekram1-node commentedon Dec 18, 2025
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
SuperSonnix71 commentedon Dec 21, 2025
@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 achoicesarray to be present. Since there's nochoicesarray in the error response, Zod validation fails immediately and throwsAI_TypeValidationError. The retry logic inretry.tsnever 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.
labolado commentedon Jan 14, 2026
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" } ]PengMai1998 commentedon Jan 16, 2026
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?
Stargazer1209 commentedon Jan 16, 2026
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.
jaybyrrd commentedon Mar 3, 2026
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?
github-actions commentedon May 3, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.