Skip to content

fix(models): keep reasoning text from vLLM, SGLang and DeepSeek in OpenAILlm - #8317

Open
sidd4848 wants to merge 4 commits into
google:mainfrom
sidd4848:fix/openai-llm-reasoning-content
Open

sidd4848 wants to merge 4 commits into
google:mainfrom
sidd4848:fix/openai-llm-reasoning-content

Conversation

@sidd4848

@sidd4848 sidd4848 commented Nov 23, 2026 •

Copy link
Copy Markdown

Link to Issue or Description of Change

1. Link to an existing issue: #8191

Problem:
OpenAI-compatible servers return a reasoning model's thinking in an extra field on the
assistant message and on each stream delta: reasoning on vLLM main, and
reasoning_content on SGLang, the DeepSeek API and older vLLM. The OpenAI SDK keeps
these as extra attributes, but OpenAILlm only read content and tool_calls. So the
reasoning never reached adk web, callbacks, session events or telemetry, even though
thoughts_token_count was already filled in.

Solution:

  • Add _extract_reasoning_text, which reads reasoning_content and then reasoning
    (the same order LiteLlm._extract_reasoning_value uses). It accepts only non-empty
    strings.
  • Non-streaming: add the reasoning as a Part(thought=True) before the answer text.
  • Streaming: emit each reasoning delta as a partial thought part, and start the final
    aggregated response with the full reasoning as one thought part.
  • The "no content + abnormal finish → error" check now ignores thought parts. A response
    that runs out of tokens mid-reasoning is still a MAX_TOKENS error, as the
    max_tokens docstring says, and isn't returned as a success with only a thought in it.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New tests in tests/unittests/integrations/openai/test_openai_llm.py use real
ChatCompletion / ChatCompletionChunk objects instead of mocks, so they also check that
the SDK exposes the extra field through attribute access:

  • non-streaming reasoning → thought part before the text, for both field names
  • non-streaming without reasoning → no thought part
  • streaming reasoning deltas → thought partials plus an aggregated final thought part, for both field names
  • reasoning only + finish_reason="length" → still a MAX_TOKENS error, streaming and non-streaming
$ pytest tests/unittests/integrations/openai
299 passed            # Python 3.11
299 passed            # Python 3.12
299 passed            # Python 3.13
299 passed            # Python 3.14

Without the source change, the 5 tests that check the new behaviour fail. The other new
tests pass on main too, because they guard behaviour that already exists.

Manual End-to-End (E2E) Tests:

Setup: a local fake Chat Completions server answers the way DeepSeek/SGLang
(reasoning_content) and vLLM (reasoning) do. A real LlmAgent with
OpenAILlm(model="deepseek-reasoner", base_url="http://127.0.0.1:<port>/v1") runs
through InMemoryRunner.run_async, with StreamingMode.NONE and StreamingMode.SSE.
The script prints every event part and then the parts stored in the session. No API key
is needed. I can attach the script if that helps.

Before (main): the reasoning is dropped.

=== field='reasoning_content' streaming=False
  [final  ] TEXT    'Paris.'
  session history: ["TEXT    'Paris.'"]

=== field='reasoning_content' streaming=True
  [partial] TEXT    'Paris.'
  [final  ] TEXT    'Paris.'
  session history: ["TEXT    'Paris.'"]

After (this PR): the reasoning arrives as thought parts and is saved in the session.

=== field='reasoning_content' streaming=False
  [final  ] THOUGHT "The user asks for France's capital. It is Paris."
  [final  ] TEXT    'Paris.'
  session history: ['THOUGHT "The user asks for France\'s capital. It is Paris."', "TEXT    'Paris.'"]

=== field='reasoning_content' streaming=True
  [partial] THOUGHT 'The user asks for '
  [partial] THOUGHT "France's capital."
  [partial] TEXT    'Paris.'
  [final  ] THOUGHT "The user asks for France's capital."
  [final  ] TEXT    'Paris.'
  session history: ['THOUGHT "The user asks for France\'s capital."', "TEXT    'Paris.'"]

=== field='reasoning' streaming=False
  [final  ] THOUGHT "The user asks for France's capital. It is Paris."
  [final  ] TEXT    'Paris.'

=== field='reasoning' streaming=True
  [partial] THOUGHT 'The user asks for '
  [partial] THOUGHT "France's capital."
  [partial] TEXT    'Paris.'
  [final  ] THOUGHT "The user asks for France's capital."
  [final  ] TEXT    'Paris.'

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules. (none)

Additional context

This PR also stops sending earlier reasoning back to the model: _content_to_openai_messages drops thought parts from outgoing assistant messages, matching OpenAIResponsesLlm, so both changes land together as requested on #8191.

No docs change is needed: this restores output users would expect, and no public API changes.

…enAILlm

OpenAI-compatible servers return reasoning in an extra field on the
message and on each stream delta (`reasoning` on vLLM main,
`reasoning_content` on SGLang, DeepSeek and older vLLM). OpenAILlm only
read `content` and `tool_calls`, so the reasoning was dropped. Map it to
`Part(thought=True)` in both the streaming and non-streaming paths, using
the same field order as LiteLlm.

A response that runs out of tokens mid-reasoning is still surfaced as a
MAX_TOKENS error, as documented on `max_tokens`.

Fixes google#8191
@google-cla

google-cla Bot commented Nov 23, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Now that reasoning is kept as thought parts, the next request turned them
into "Thought: ..." text inside the assistant message, so the model saw its
earlier private reasoning as part of its visible answer. Drop thought parts
from outgoing assistant messages, as OpenAIResponsesLlm already does.

Refs google#8191
@yibol9768-alt

Copy link
Copy Markdown

Reviewed this against my local fix and tests for #8191 — the parsing side looks solid across both non-streaming and streaming paths (reasoning_content / reasoning extraction, thought-before-answer ordering, and keeping reasoning-only abnormal finishes as MAX_TOKENS errors).

One substantive item to fold in before merge, per @sanketpatil06's comment on #8191 (#8191 (comment)):

  • Drop replayed thought parts on outgoing assistant messages in _content_to_openai_messages / _part_to_openai_content (src/google/adk/integrations/openai/_openai_llm.py:89-90,152-153) instead of formatting them as "Thought: <text>", matching OpenAIResponsesLlm. Worth checking two edge cases in tests:
    1. An assistant turn with [Part(thought=True, text=...), Part.from_text(...)] or [Part(thought=True, text=...), Part(function_call=...)] replays only the answer text / tool call with no "Thought:" prefix.
    2. An assistant turn containing only thought parts is omitted rather than emitting an empty assistant message.

Two minor test-coverage suggestions for the parsing side:

  • A message/delta that sets both reasoning_content and reasoning (verifying only one is used without duplication).
  • A single stream chunk carrying both a reasoning delta and content (or a message with reasoning + tool_calls).

(Also a heads-up that the google-cla check is currently failing and will need the CLA signed before maintainers can merge.)

…deltas

Add the cases raised in review: a message or delta that sets both
reasoning_content and reasoning yields a single thought, a message with
reasoning and tool_calls keeps both in order, and a stream chunk carrying
reasoning and answer text yields a thought partial then a text partial.

Refs google#8191
@sidd4848

Copy link
Copy Markdown
Author

Thanks @yibol9768-alt for the review, and @sanketpatil06 for the direction on #8191. Both are addressed:

Replay of thought parts (220f022)
_content_to_openai_messages now drops thought parts from outgoing assistant messages instead of sending them as "Thought: ...", matching OpenAIResponsesLlm. Tests cover:

  • thought + answer text → only the text is sent
  • thought + tool call → only the tool call is sent, with no empty content
  • an assistant turn with only thoughts → no message at all
  • a thought parsed from one response isn't sent in the next request

Extra parsing coverage (75a40c5)

  • reasoning_content and reasoning both set, on a message and on a stream delta → one thought, from reasoning_content
  • reasoning + tool_calls on one message → thought, then the function call
  • one stream chunk with reasoning and content → thought partial, then text partial

pytest tests/unittests/integrations/openai: 307 passed on Python 3.11, 3.12, 3.13 and 3.14.

E2E, two turns in one session (same fake server and InMemoryRunner as in the description). Messages sent in the second request:

Before:

user      "Capital of France?"
assistant "Thought: The user asks for France's capital. It is Paris.\nParis."
user      "And Spain?"

After:

user      "Capital of France?"
assistant "Paris."
user      "And Spain?"

It's the same with StreamingMode.SSE.

On the CLA: cla/google passes on this PR. The bot's comment above was posted before I signed.

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.

OpenAILlm drops reasoning text returned by vLLM, SGLang and DeepSeek (reasoning / reasoning_content)

2 participants