Python: Normalize SDK role enums to str when creating Messages - #7566
Open
westey (westey-m) wants to merge 2 commits into
Open
Python: Normalize SDK role enums to str when creating Messages#7566westey (westey-m) wants to merge 2 commits into
westey (westey-m) wants to merge 2 commits into
Conversation
westey (westey-m)
temporarily deployed
to
github-app-auth
August 7, 2026 09:25 — with
GitHub Actions
Inactive
westey (westey-m)
temporarily deployed
to
github-app-auth
August 7, 2026 09:25 — with
GitHub Actions
Inactive
westey (westey-m)
temporarily deployed
to
github-app-auth
August 7, 2026 09:25 — with
GitHub Actions
Inactive
westey (westey-m)
marked this pull request as ready for review
August 7, 2026 09:27
westey (westey-m)
temporarily deployed
to
github-app-auth
August 7, 2026 09:27 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
Pull request overview
Normalizes SDK role enums to plain strings before creating framework messages, preventing durable session serialization failures.
Changes:
- Adds provider-local role normalization.
- Applies normalization across Foundry and Azure AI Search conversion paths.
- Adds regression tests for enum-backed roles and session serialization.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py |
Normalizes roles during SDK message conversion. |
python/packages/foundry_hosting/tests/test_responses.py |
Tests role conversion and session behavior. |
python/packages/azure-ai-search/agent_framework_azure_ai_search/_context_provider.py |
Normalizes Knowledge Base response roles. |
python/packages/azure-ai-search/tests/test_aisearch_context_provider.py |
Tests Knowledge Base enum-role conversion. |
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Agent Framework Review — Iteration 1
Completed passes: 5 | Result: No high-severity findings
Scope: full PR (1 commit(s)): b392d53f048e
Review passes
- Correctness (
gpt-5.6-sol) — No issues found in this pass. - Security Reliability (
claude-opus-4.8) — No issues found in this pass. - Test Coverage (
gpt-5.6-sol) — No issues found in this pass. - Failure Modes (
claude-opus-4.8) — No issues found in this pass. - Design Approach (
claude-opus-4.8) — No issues found in this pass.
AgentSession.to_dict() does not run the durable-state validator, so the previous round-trip assertion passed even with an enum role left in place. Persist through FileSessionStore.set/get instead, which validates state on save, and assert the restored role's exact type. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 3a5efc79-5c78-40b8-a1c5-c0f84e0795e1
westey (westey-m)
temporarily deployed
to
github-app-auth
August 7, 2026 09:53 — with
GitHub Actions
Inactive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation & Context
Running an agent as a Foundry hosted agent fails to persist session state when the conversation
came in over the Azure Responses API:
MessageRoleinazure.ai.agentserver.responses.modelsis astr-subclass enum. The hostinglayer passes it straight through into
Message(role=...), but Agent Framework typesMessage.roleas a plainstr(Role = NewType("Role", str)), and durable session-stateserialization identifies scalars by exact type (
type(value) in _STATE_SCALAR_TYPES) rather thanisinstance. Astrsubclass therefore misses the scalar path and is rejected as unserializable.The result is that any hosted agent using a session store breaks as soon as it tries to save
history — the messages look like ordinary strings right up to the point of serialization. This
change makes the role a genuine
strat the boundary where the SDK type enters the framework, sosessions round-trip normally.
Description & Review Guide
What are the major changes?
Each provider now normalizes the role to a plain
strat the point it constructs aMessage,via a small module-local helper (
role.value if isinstance(role, Enum) else role):foundry_hosting/_responses.py— applied at all five SDK→Messageconversion sites across_item_to_messageand_output_item_to_message.azure-ai-search/_context_provider.py— applied tokb_msg.rolewhen parsing a KnowledgeBase retrieval response, preserving the existing
or "assistant"fallback.Four regression tests are added, including one that asserts a converted message survives an
AgentSessionserialization round-trip.What is the impact of these changes?
Hosted agents can save and restore session state again. Not a breaking change: the normalized
value compares equal to the enum member it replaces, so
msg.role == "user"and any stringhandling behave exactly as before — only the runtime type narrows from a subclass to
str,which is what the public type annotation already promised. Core is untouched.
Note that
foundry_hostingdeliberately preservesMessageRolein workflow checkpointsthrough the
_AZURE_RESPONSES_MESSAGE_ROLE_TYPEallowlist; that path is unaffected and itsexisting tests still pass.
What do you want reviewers to focus on?
Whether normalizing per provider is the right layer, versus making core's serializer accept
scalar subclasses. Both were considered; the per-provider fix was chosen so that
Message.roleactually holds the type it is annotated with, rather than papering over the mismatch at the
serialization boundary. Also worth a look: whether other providers construct
Messagefrom anSDK role type and were missed — a survey suggested not (
a2aalready maps to literals,azure-cosmos-memoryalready unwraps defensively, MCP'sRoleis aLiteral), but a secondpair of eyes would help.
Related Issue
Related to #6361
This was found while building the harness samples tracked by that issue, but it is a
general-purpose fix in the provider packages and does not complete the sample/blog work — so it is
linked without a closing keyword intentionally, and #6361 should stay open. There is no other open
PR for this change.
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.