Skip to content

Backport: fix(openai-compatible): accept empty string role in streaming delta chunks#15570

Merged
aayush-kapoor merged 1 commit into
release-v6.0from
backport-pr-15464-to-release-v6.0
May 22, 2026
Merged

Backport: fix(openai-compatible): accept empty string role in streaming delta chunks#15570
aayush-kapoor merged 1 commit into
release-v6.0from
backport-pr-15464-to-release-v6.0

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

This is an automated backport of #15464 to the release-v6.0 branch. FYI @kagura-agent

…hunks (#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 <83492835+aayush-kapoor@users.noreply.github.com>
@github-actions github-actions Bot enabled auto-merge (squash) May 22, 2026 21:33
auto-merge was automatically disabled May 22, 2026 21:34

Pull request was closed

@aayush-kapoor aayush-kapoor reopened this May 22, 2026
@aayush-kapoor aayush-kapoor enabled auto-merge (squash) May 22, 2026 21:34
@aayush-kapoor aayush-kapoor merged commit e40e1d4 into release-v6.0 May 22, 2026
47 checks passed
@aayush-kapoor aayush-kapoor deleted the backport-pr-15464-to-release-v6.0 branch May 22, 2026 21:40
@github-actions
Copy link
Copy Markdown
Contributor Author

🚀 Published in:

Package Version
ai 6.0.191 github npm
@ai-sdk/alibaba 1.0.25 github npm
@ai-sdk/angular 2.0.192 github npm
@ai-sdk/baseten 1.0.51 github npm
@ai-sdk/cerebras 2.0.54 github npm
@ai-sdk/deepinfra 2.0.52 github npm
@ai-sdk/fireworks 2.0.53 github npm
@ai-sdk/gateway 3.0.120 github npm
@ai-sdk/google-vertex 4.0.137 github npm
@ai-sdk/huggingface 1.0.50 github npm
@ai-sdk/langchain 2.0.197 github npm
@ai-sdk/llamaindex 2.0.191 github npm
@ai-sdk/moonshotai 2.0.23 github npm
@ai-sdk/openai-compatible 2.0.48 github npm
@ai-sdk/react 3.0.193 github npm
@ai-sdk/rsc 2.0.191 github npm
@ai-sdk/svelte 4.0.191 github npm
@ai-sdk/togetherai 2.0.53 github npm
@ai-sdk/vercel 2.0.50 github npm
@ai-sdk/vue 3.0.191 github npm
@ai-sdk/xai 3.0.92 github npm

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant