Skip to content

Pass chat history to agents as structured messages#22607

Merged
mvdbeek merged 4 commits intogalaxyproject:devfrom
dannon:agent-router-message-history
May 1, 2026
Merged

Pass chat history to agents as structured messages#22607
mvdbeek merged 4 commits intogalaxyproject:devfrom
dannon:agent-router-message-history

Conversation

@dannon
Copy link
Copy Markdown
Member

@dannon dannon commented Apr 30, 2026

The chat router was flattening prior turns into a "Previous conversation:\nrole: content\n..." text blob and prepending it to the user's query. pydantic-ai already supports a structured message_history parameter on agent.run(), and ChatManager.get_chat_history(format_for_pydantic_ai=True) already returns a list[ModelMessage] -- the API just wasn't using it.

This switches the chat API to the structured path and plumbs message_history through BaseGalaxyAgent.process() and the sub-agent calls inside _call_agent_from_tool. The router's _build_query_with_context text-concat goes away, along with the magic max_history = 6 cap; truncation now goes through truncate_message_history() (first message + last 40, configurable via MAX_HISTORY_MESSAGES). Sub-agents called from inside a tool get a tighter TOOL_HELPER_HISTORY_MESSAGES = 8 window since tool-context turns burn token budget faster.

_extract_message_history() accepts both shapes: pydantic-ai ModelMessage objects from the chat API path, and legacy {role, content} dicts from any direct caller still on the old shape. Malformed entries are logged and dropped rather than crashing the run.

Test plan

  • pytest test/unit/app/test_agents.py::TestAgentUnitMocked -- 30 passed (added 9 new tests covering truncation boundaries, legacy-shape coercion, unsupported-shape rejection, router passthrough, fresh-conversation path)
  • Manual smoke: run a multi-turn ChatGXY conversation and confirm follow-up turns reference earlier context

dannon added 3 commits April 29, 2026 22:57
The chat router was flattening prior conversation messages into a text
blob via _build_query_with_context, which loses the role/content
semantics chat models already know how to use. The structured pydantic-ai
message format already exists -- ChatManager.get_chat_history has a
format_for_pydantic_ai flag returning ModelMessage objects -- but the
chat API was calling it with False and the router was re-flattening the
result.

Flips the API call to format_for_pydantic_ai=True, plumbs the structured
list[ModelMessage] through to agent.run(message_history=...), and adds
truncate_message_history that keeps the first message + last
MAX_HISTORY_MESSAGES (40) so original intent is preserved across long
conversations. Tighter cap (8) for sub-agent calls from inside
@agent.tool since tool-context turns burn token budget faster.

Drops the magic max_history=6 from router._build_query_with_context and
the 200-char-per-message text-concat from BaseGalaxyAgent's
_call_agent_from_tool.
CLAUDE.md is explicit that test files shouldn't use inline imports.
The pydantic_ai.messages and truncate_message_history imports were
duplicated across seven new tests; move them to module level (under
the existing pytest.importorskip guard so the skip behavior is
preserved when pydantic_ai isn't installed).
Mypy on CI runs against test files too (not just lib/), and list
invariance tripped it up: ``history = [ModelRequest(...), ModelResponse(...)]``
gets inferred as ``list[ModelRequest]``, which can't be passed to a
function expecting ``list[ModelMessage]``. Annotate the four test
fixtures explicitly.
@mvdbeek mvdbeek merged commit 4c46425 into galaxyproject:dev May 1, 2026
57 of 59 checks passed
@github-project-automation github-project-automation Bot moved this from Needs Review to Done in Galaxy Dev - weeklies May 1, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 1, 2026

This PR was merged without a "kind/" label, please correct.

@itisAliRH itisAliRH deleted the agent-router-message-history branch May 4, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

3 participants