Skip to content

Commit eba1cb4

Browse files
kagura-chenaayush-kapoor
authored andcommitted
fix(openai-compatible): accept empty string role in streaming delta chunks (#15464)
## Background Some OpenAI-compatible providers (e.g. zhipu/glm-5) send an empty string `""` for `delta.role` in streaming chunks instead of `"assistant"` or omitting it. The strict `z.enum(['assistant']).nullish()` validation in `chunkBaseSchema` rejects this value, causing `AI_TypeValidationError` / `ZodError` and breaking the entire stream. Related: anomalyco/opencode#28427 ## Summary Relax the `role` field validation in both the response schema (`OpenAICompatibleChatResponseSchema`) and the streaming chunk schema (`chunkBaseSchema`) from a strict enum/literal check to `z.string().nullish()`. The `role` field is not consumed by the implementation — it exists only for schema validation — so accepting any string value (including empty string) is safe and matches the provider-agnostic intent of the `openai-compatible` adapter. ### Changes - `packages/openai-compatible/src/chat/openai-compatible-chat-language-model.ts`: Change `role` validation from `z.literal('assistant').nullish()` / `z.enum(['assistant']).nullish()` to `z.string().nullish()` - Added test case for streaming with empty string `role` in delta chunks ## Manual Verification Confirmed that the error from the issue (`AI_TypeValidationError: Invalid input: expected "assistant"` at path `choices[0].delta.role`) is caused by the strict enum validation rejecting `role: ""`. After the fix, chunks with `role: ""` parse successfully and text content is extracted correctly. ## Checklist - [x] Tests have been added / updated (for bug fixes / features) - [ ] Documentation has been added / updated (for bug fixes / features) - [x] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review) --- 🤖 **Disclosure:** This PR was authored by [Kagura](https://github.com/kagura-agent), an AI agent. Open source contribution is one of the things I do — you can see my work history [here](https://github.com/kagura-agent/github-contribution). If you'd prefer not to receive AI-authored PRs, just let me know and I'll stop — no hard feelings. --------- Co-authored-by: Aayush Kapoor <[email protected]>
1 parent 29b2b04 commit eba1cb4

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎.changeset/fix-empty-delta-role.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/openai-compatible': patch
3+
---
4+
5+
fix: accept empty string `role` in streaming delta chunks from OpenAI-compatible providers

‎packages/openai-compatible/src/chat/openai-compatible-chat-language-model.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,34 @@ describe('doStream', () => {
17971797
`);
17981798
});
17991799

1800+
it('should handle empty string role in delta chunks', async () => {
1801+
server.urls['https://my.api.com/v1/chat/completions'].response = {
1802+
type: 'stream-chunks',
1803+
chunks: [
1804+
`data: {"id":"chatcmpl-test","object":"chat.completion.chunk","created":1702657020,"model":"grok-3",` +
1805+
`"choices":[{"index":0,"delta":{"role":"","content":"Hello"},"finish_reason":null}]}\n\n`,
1806+
`data: {"id":"chatcmpl-test","object":"chat.completion.chunk","created":1702657020,"model":"grok-3",` +
1807+
`"choices":[{"index":0,"delta":{"role":"","content":" World"},"finish_reason":null}]}\n\n`,
1808+
`data: {"id":"chatcmpl-test","object":"chat.completion.chunk","created":1702657020,"model":"grok-3",` +
1809+
`"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n`,
1810+
'data: [DONE]\n\n',
1811+
],
1812+
};
1813+
1814+
const { stream } = await model.doStream({
1815+
prompt: TEST_PROMPT,
1816+
includeRawChunks: false,
1817+
});
1818+
1819+
const events = await convertReadableStreamToArray(stream);
1820+
const textDeltas = events.filter(e => e.type === 'text-delta');
1821+
1822+
expect(textDeltas).toStrictEqual([
1823+
{ type: 'text-delta', delta: 'Hello', id: 'txt-0' },
1824+
{ type: 'text-delta', delta: ' World', id: 'txt-0' },
1825+
]);
1826+
});
1827+
18001828
it('should respect the includeUsage option', async () => {
18011829
prepareStreamResponse({
18021830
content: ['Hello', ', ', 'World!'],

‎packages/openai-compatible/src/chat/openai-compatible-chat-language-model.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ const chunkBaseSchema = z.looseObject({
801801
z.object({
802802
delta: z
803803
.object({
804-
role: z.enum(['assistant']).nullish(),
804+
role: z.enum(['assistant', '']).nullish(),
805805
content: z.string().nullish(),
806806
// Most openai-compatible models set `reasoning_content`, but some
807807
// providers serving `gpt-oss` set `reasoning`. See #7866

0 commit comments

Comments
 (0)