Skip to content

fix: handle reasoning_content in OpenRouter thinking model responses#6014

Open
lireno wants to merge 1 commit into
crewAIInc:mainfrom
lireno:fix/openrouter-reasoning-content
Open

fix: handle reasoning_content in OpenRouter thinking model responses#6014
lireno wants to merge 1 commit into
crewAIInc:mainfrom
lireno:fix/openrouter-reasoning-content

Conversation

@lireno
Copy link
Copy Markdown

@lireno lireno commented Jun 2, 2026

Summary

When using OpenRouter with thinking models (Anthropic Claude, Google Gemini), the API returns a reasoning_content field alongside content in streaming deltas and response messages. The OpenAI completion handlers were only checking the content field, causing empty responses and ValueError: Invalid response from LLM call - None or empty.

Changes

  • Streaming path (sync and async): Check reasoning_content as fallback when content is not a string in delta chunks
  • Non-streaming path (sync and async): Check reasoning_content as fallback when message.content is not a string

Testing

All 121 existing tests pass (OpenAI completions and OpenAI-compatible providers).

Fixes #5537

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved OpenAI chat completion handling to support models that emit alternative response formats, with fallback mechanisms for both streaming and non-streaming requests. Enhanced compatibility and reliability across different model types.

When using OpenRouter with thinking models (Anthropic Claude, Google
Gemini), the API returns a reasoning_content field alongside content in
streaming deltas and response messages. The OpenAI completion handlers
were only checking the content field, causing empty responses and
ValueError when reasoning_content contained the actual response text.

This fix adds fallback to reasoning_content in both streaming and
non-streaming paths (sync and async), gated with isinstance(str)
checks for robustness with mock objects.

Fixes crewAIInc#5537

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 2, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e3208b86-cd64-4879-9b99-8b58c61673a8

📥 Commits

Reviewing files that changed from the base of the PR and between c81b4fe and 295fa71.

📒 Files selected for processing (1)
  • lib/crewai/src/crewai/llms/providers/openai/completion.py

📝 Walkthrough

Walkthrough

OpenAI completion handling now supports models that emit reasoning_content instead of or alongside standard content. Non-streaming and streaming paths in both sync and async variants apply consistent fallback logic: preferring content when available, otherwise using reasoning_content, and defaulting to an empty string if neither is present.

Changes

OpenAI completion reasoning_content fallback

Layer / File(s) Summary
Non-streaming completion fallback
lib/crewai/src/crewai/llms/providers/openai/completion.py
Synchronous and asynchronous non-streaming paths now derive final content from message.content when it is a string; if not, fall back to message.reasoning_content when that is a string; otherwise default to an empty string.
Streaming completion fallback
lib/crewai/src/crewai/llms/providers/openai/completion.py
Synchronous and asynchronous streaming paths now extract per-chunk delta_text by preferring chunk_delta.content when it is a string, otherwise using chunk_delta.reasoning_content when that is a string, and only appending or emitting when delta_text is present.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 Reasoning thoughts, now out in the open,
CrewAI's ears are listening,
Content or whispers—the model has spoken,
Each streaming chunk, a thought glistening!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding support for reasoning_content in OpenAI completion handling, which directly addresses the OpenRouter thinking model response issue.
Linked Issues check ✅ Passed The PR addresses the core coding requirement from issue #5537: robustly handling reasoning_content field when content is unavailable in OpenRouter thinking model responses for both streaming and non-streaming paths.
Out of Scope Changes check ✅ Passed All changes are focused on OpenAI completion handlers to support reasoning_content fallback, which is directly scoped to fixing OpenRouter thinking model response handling without introducing unrelated modifications.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

[BUG] Problems with OpenRouter thinking models

1 participant