Skip to content

Python: preserve roles in Claude prompt history - #1

Closed
jpalvarezl wants to merge 3 commits into
mainfrom
jpalvarezl/fix/7894_claude_flatten_turn
Closed

jpalvarezl wants to merge 3 commits into
mainfrom
jpalvarezl/fix/7894_claude_flatten_turn

Conversation

@jpalvarezl

@jpalvarezl jpalvarezl commented Sep 4, 2026 •

Copy link
Copy Markdown
Owner

Motivation & Context

ClaudeAgent currently 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

  • What are the major changes? Update _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 documented SequentialBuilder sample that passes a grammar inspector's output to a second Claude agent. Broaden the function_invocation_kwargs and client_kwargs annotations from dict to Mapping in the run() signatures.
  • What is the impact of these changes? Claude receives explicit role boundaries instead of undifferentiated text. The transcript is still sent as one SDK user message; this does not introduce native multi-role history replay or shared Claude sessions. Labels preserve message roles, not individual agent names. Existing single-user-message behavior remains unchanged.
  • What do you want reviewers to focus on? Whether the transcript framing clearly distinguishes supplied history from the receiving agent's instructions, whether the formatting coverage captures agent-to-agent handoff scenarios, and whether the sequential sample demonstrates the intended behavior.

Related Issue

Fixes microsoft#7894

This overlaps with microsoft#7895, which also introduces role labels. This PR additionally provides explicit conversation-history framing, broader formatting coverage, a sequential-agent sample, and the Mapping annotation updates.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue, and the overlapping open PR and differences are explained in Related Issue above.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

@jpalvarezl
jpalvarezl marked this pull request as ready for review September 7, 2026 12:38
@jpalvarezl jpalvarezl changed the title Python: preserve authors in Claude prompts Python: preserve roles in Claude prompt history Sep 7, 2026
@jpalvarezl jpalvarezl closed this Sep 7, 2026
jpalvarezl pushed a commit that referenced this pull request Sep 28, 2026
…osoft#8201) (microsoft#8284)

* Python: add public TypedDict for AgentExecutor checkpoint state (microsoft#8201)

Expose AgentExecutorCheckpointState / AgentSessionCheckpointState, validate restore field types, and cover partial/malformed/forward-compatible payloads.

* fix(checkpoint): address microsoft#8284 review on session TypedDict ownership (#1)

- Define AgentSessionDict on AgentSession (to_dict return) so the shape is
  not duplicated in AgentExecutor; keep AgentSessionCheckpointState as alias.
- Include ServiceSessionId mapping in service_session_id.
- Type on_checkpoint_restore as AgentExecutorCheckpointState only.
- Sync root stub exports (__init__.pyi) with runtime __all__.

* fix(checkpoint): validate AgentExecutor restore element types (microsoft#8284)

Enforce Message/Content element types, string pending-request keys, and
required agent_session.session_id so malformed checkpoints fail at restore.

* fix(checkpoint): validate AgentSessionDict fields on restore

- Validate agent_session.state / service_session_id types
- Raise WorkflowCheckpointException when session restore fails
- Drop unshipped AgentSessionCheckpointState alias; use AgentSessionDict

* fix(checkpoint): keep session/executor checkpoint payloads as dict

TypedDict schemas remain public for documentation and validation, but to_dict and on_checkpoint_save/restore stay dict[str, Any] so subclasses, samples, and pyright stay compatible.

* fix(checkpoint): keep AgentSessionDict optional keys under postponed annotations

Use a required base plus total=False subclass, stop exporting AgentSessionDict from the public package root, and tighten AgentExecutor restore validation with walrus checks.

---------

Co-authored-by: minelhi <3417378192@qq.com>
Co-authored-by: LI <2484593937@qq.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>

This branch had an error being deployed

1 failed deployment
github-app-auth — 345c5eb8 Deployed Sep 7, 2026 by jpalvarezl via add_label #7
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.

Python: [Bug]: ClaudeAgent flattens multi-agent conversation history into one undifferentiated user turn

1 participant