Repository navigation
Commit eba1cb4
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
- packages/openai-compatible/src/chat
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1797 | 1797 | | |
1798 | 1798 | | |
1799 | 1799 | | |
| 1800 | + | |
| 1801 | + | |
| 1802 | + | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
| 1813 | + | |
| 1814 | + | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
| 1822 | + | |
| 1823 | + | |
| 1824 | + | |
| 1825 | + | |
| 1826 | + | |
| 1827 | + | |
1800 | 1828 | | |
1801 | 1829 | | |
1802 | 1830 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
801 | 801 | | |
802 | 802 | | |
803 | 803 | | |
804 | | - | |
| 804 | + | |
805 | 805 | | |
806 | 806 | | |
807 | 807 | | |
| |||
0 commit comments