Skip to content

Keep model reasoning in the saved agent trajectory - #346

Open
yilunzhao wants to merge 3 commits into
mainfrom
yilun/trajectory-reasoning
Open

yilunzhao wants to merge 3 commits into
mainfrom
yilun/trajectory-reasoning

Conversation

@yilunzhao

Copy link
Copy Markdown
Contributor

The openai-agents SDK converts Chat Completions reasoning_content into a ReasoningItem, but _convert_trajectory only handled message, tool call and tool output items, so reasoning was dropped before the trajectory was saved. AgentTurn gains an optional reasoning field and the converter a ReasoningItem branch that attaches the item to the assistant turn that follows it, flushing onto an otherwise empty assistant turn at a tool output or the end of the run so it never crosses a response boundary. Turns without reasoning serialize exactly as before: trajectory.py, openai_agents.py.

vLLM returns thinking as reasoning, a field the pinned SDK's Chat Completions converter does not read, so behind a vLLM server the new branch would still see nothing. The scaffold therefore instantiates a small subclass of OpenAIChatCompletionsModel that mirrors reasoning onto reasoning_content on the raw completion before the SDK converts it, and does nothing when a non-empty reasoning_content or thinking blocks are already present or when there is no reasoning. The SDK releases that read the field themselves require openai 3.x, which this repository's pins exclude, so the mirror stays until that migration.

Checked on Beaker with Qwen3.5-9B behind vLLM 0.19.1 (--reasoning-parser qwen3, thinking enabled) on three litsearch instances: all seven model responses produced reasoning and all seven are saved on the assistant turns, with none of that text in any turn content, tool argument or final output. Also checked with a one-instance litsearch run against deepseek-reasoner, and end to end through the SDK Runner over a mock transport answering in vLLM 0.19.1's message shape, with nothing extra replayed to the server: test_openai_agents_scaffold.py, test_trajectory_types.py. The new scaffold tests skip in CI like the existing ones in that file.

The openai-agents SDK already turns Chat Completions reasoning_content
into a ReasoningItem, but _convert_trajectory had no branch for it, so
the reasoning was dropped before the trajectory was saved. Add an
optional AgentTurn.reasoning field, attach each reasoning item to the
assistant turn that follows it (message or tool call), and keep trailing
reasoning on an empty assistant turn instead of losing it. Pending
reasoning never crosses a response boundary: a message without text or a
tool output flushes it the same way. Turns without reasoning serialize
exactly as before. A reasoning-only final response now yields one empty
assistant turn, so num_turns is one higher in that cut-off case.
vLLM returns a reasoning model's thinking as the message field reasoning.
openai-agents 0.20 builds a ReasoningItem only from reasoning_content or
thinking blocks, so behind a vLLM server the converter never sees the
reasoning and the saved trajectory still drops it.

The scaffold now instantiates a small subclass of OpenAIChatCompletionsModel
whose _fetch_response mirrors reasoning onto reasoning_content on the raw
completion, before the SDK converts the message. The mirror does nothing when
the server already returned a non-empty reasoning_content or thinking blocks,
or when the message carries no reasoning at all. Streaming responses pass
through untouched; olmo-eval does not use them and the SDK's stream handler
already reads the field on that path.

The SDK replays reasoning_content into later requests only for model names
containing "deepseek", so a DeepSeek-named model served by vLLM sends the
mirrored text back as assistant history. vLLM 0.19.1 ignores that key, and
a test pins the replay for a DeepSeek name and its absence for a Qwen name.

openai-agents 0.21.1 and later read the reasoning field themselves but
require openai 3.x, which this repository's pins exclude, so the mirror
stays until that migration and should be removed with it. A test pins the
private hook so a rename fails loudly instead of silently losing reasoning
again.
@yilunzhao
yilunzhao requested a review from undfined September 2, 2026 02:14

This branch has not been deployed

No deployments
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.

1 participant