Skip to content

feat: add reasoning token support to OpenRouter integration#3264

Open
ArkaD171717 wants to merge 5 commits into
deepset-ai:mainfrom
ArkaD171717:openrouter-reasoning-support
Open

feat: add reasoning token support to OpenRouter integration#3264
ArkaD171717 wants to merge 5 commits into
deepset-ai:mainfrom
ArkaD171717:openrouter-reasoning-support

Conversation

@ArkaD171717
Copy link
Copy Markdown
Contributor

@ArkaD171717 ArkaD171717 commented May 2, 2026

OpenRouter returns reasoning/thinking content as extra fields on the completion
response (reasoning_details). The parent OpenAIChatGenerator doesn't know
about these fields, so they get silently dropped

This overrides run(), run_async(), and the stream handler to pull out
reasoning content and attach it to ChatMessage via ReasoningContent
Also handles multi-turn: to_openai_dict_format() strips reasoning, so
_prepare_api_call re-injects reasoning_details into the formatted message
dicts before sending them back to the API

Extraction uses getattr since OpenRouter attaches these as extra attrs on
standard OpenAI SDK models, not typed fields

19 new tests covering sync, async, streaming, multi-turn, edge cases
(empty messages, logprobs, malformed tool call JSON, empty streaming choices,
async streaming with reasoning)
All 17 existing tests still pass

Closes #2181

Extract reasoning/thinking content from OpenRouter responses and attach
to ChatMessage via ReasoningContent. Override run(), run_async(), and
stream handler. Handle multi-turn by re-injecting reasoning_details
into formatted message dicts.

13 new tests covering extraction, conversion, streaming, multi-turn,
edge cases, and async paths.

Closes deepset-ai#2181
@ArkaD171717 ArkaD171717 requested a review from a team as a code owner May 2, 2026 09:30
@ArkaD171717 ArkaD171717 requested review from julian-risch and removed request for a team May 2, 2026 09:30
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 2, 2026

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

Heads-up for maintainers

This PR is from a fork and touches integrations whose integration tests require API keys.
Those tests are skipped in CI because fork PRs don't have access to repo secrets for security reasons.

Affected integrations:

  • openrouter

Please run the integration tests locally (hatch run test:integration inside each folder) before approving.

@github-actions github-actions Bot added the type:documentation Improvements or additions to documentation label May 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 2, 2026

Coverage report (openrouter)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/openrouter/src/haystack_integrations/components/generators/openrouter/chat
  chat_generator.py 82, 146-147, 205, 218, 427, 487, 549, 587-589
Project Total  

This report was generated by python-coverage-comment-action

ArkaD171717 and others added 4 commits May 2, 2026 13:19
Add tests for empty messages, logprobs, malformed tool call JSON,
empty streaming choices, async empty messages, and async streaming
with reasoning content.
logprobs Choice requires top_logprobs field, was missing.
Inline imports in test methods trigger PLC0415 lint error.
@ArkaD171717
Copy link
Copy Markdown
Contributor Author

Brought up to date

@ArkaD171717
Copy link
Copy Markdown
Contributor Author

@julian-risch could you take a look at this PR when you get the time please?

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

Labels

integration:openrouter type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenRouter - reasoning support

2 participants