fix(llm): ensure json keyword in messages for Alibaba/Qwen compatibility#664
fix(llm): ensure json keyword in messages for Alibaba/Qwen compatibility#664kevin-ho wants to merge 1 commit into
Conversation
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughAdds an internal helper that ensures the case-insensitive token "json" appears in chat messages when ChangesJSON Keyword Enforcement for OpenAI-Compatible Providers
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/llm/backends/openai.py`:
- Around line 41-46: The loop that mutates system messages assumes
updated["content"] is a string and calls rstrip(), causing AttributeError for
multimodal content lists; update the mutation in the for-loop (where result,
msg, updated and _JSON_KEYWORD_HINT are used) to safely coerce or normalize
content to a string before calling rstrip() (e.g., convert list/other types via
str(...) or serialize text blocks) and then append f"\n\n{_JSON_KEYWORD_HINT}"
so the mutation path mirrors the earlier detection path and never calls rstrip()
on a non-str.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 96498046-60ad-45a5-bb6c-d6b88f063ac3
📒 Files selected for processing (1)
src/llm/backends/openai.py
When json_mode=True is set, Alibaba/Qwen providers strictly enforce the OpenAI spec requirement that the word 'json' must appear in messages. Most providers are lenient about this check but Qwen rejects the call with a 400 error. Adds _ensure_json_keyword() helper that appends a brief instruction to the system message when json_mode is active and no message contains the keyword. Gated on json_mode specifically to avoid noise for structured output calls to providers that don't need it. Ref plastic-labs#663
04d76ec to
e43cd0b
Compare
|
Thanks for the review @coderabbitai[bot] — I looked into the This is a false positive. The
The detection path (line 36) uses No changes needed. |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 19068
The branches are mutually exclusive, so a list-typed |
Closes #663
When
json_mode=Trueis set, Alibaba/Qwen providers strictly enforce the OpenAI spec requirement that the word "json" must appear in messages. Most providers are lenient about this check but Qwen rejects the call with a 400.Adds
_ensure_json_keyword()helper that appends a brief instruction to the system message whenjson_modeis active and no message contains the keyword. Gated onjson_modespecifically to avoid noise for structured output calls to providers that don't need it.Changes:
_JSON_KEYWORD_HINTand helper_ensure_json_keyword()insrc/llm/backends/openai.pycomplete()andstream()whenextra_params["json_mode"]is setTesting:
Verified against
opencode-go/qwen3.6-plusvia OmniRoute gateway — previously 400'd immediately, now succeeds on first attempt.Summary by CodeRabbit