Python: preserve roles in Claude prompt history - #8122
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Unescaped message text can spoof role boundaries, and the sample summary is not module-level.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Preserves role context when Claude receives multi-message history and adds a sequential-agent example.
Changes:
- Adds role-labeled transcript formatting while preserving single-user prompts.
- Broadens runtime keyword argument types to
Mapping. - Adds regression tests and an Anthropic sequential workflow sample.
File summaries
| File | Description |
|---|---|
python/packages/claude/agent_framework_claude/_agent.py |
Formats multi-message prompts with role labels and updates annotations. |
python/packages/claude/tests/test_claude_agent.py |
Covers role-aware prompt formatting. |
python/samples/02-agents/providers/anthropic/anthropic_claude_sequential_agents.py |
Demonstrates Claude agent handoff. |
python/samples/02-agents/providers/anthropic/README.md |
Lists the new sample. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: Findings reported
Scope: full PR (3 commit(s)): ea6f08b2c7d3, ef5fc035173a, 345c5eb86b99
Model: gpt-5.6-sol-fast
Overview
The review found 2 verified inline finding(s).
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
2 verified findings remained after source verification (2 medium) across 1 file. Details are attached to the affected lines below.
Affected areas: python/packages/claude/agent_framework_claude/_agent.py
Motivation & Context
ClaudeAgentcurrently flattens incoming messages into a single prompt without preserving their roles. In multi-agent workflows, this makes user input and previous agents' responses indistinguishable. This change retains role information and explicitly frames the supplied transcript as conversation history for the receiving agent's assigned task.Description & Review Guide
_format_prompt()to prepend a conversation-history explanation and label each message with its role, while leaving a single user message unchanged. Add regression coverage for mixed-role history, messages from named and unnamed agents, and a single assistant message. Add a documentedSequentialBuildersample that passes a grammar inspector's output to a second Claude agent. Broaden thefunction_invocation_kwargsandclient_kwargsannotations fromdicttoMappingin therun()signatures.Related Issue
Fixes #7894
This overlaps with #7895, which also introduces role labels. This PR additionally provides explicit conversation-history framing, broader formatting coverage, a sequential-agent sample, and the
Mappingannotation updates.Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.