Skip to content

Python: fix(workflows): support Literal types in workflow edge type compatibility - #9396

Closed
Nithin (nithin42) wants to merge 1 commit into
microsoft:mainfrom
nithin42:fix/workflow-literal-type-compatibility
Closed

Nithin (nithin42) wants to merge 1 commit into
microsoft:mainfrom
nithin42:fix/workflow-literal-type-compatibility

Conversation

@nithin42

Copy link
Copy Markdown
Contributor

Summary

Fixes #9354.

Resolves an issue where WorkflowBuilder.build() rejected edges from an executor outputting a Literal type (e.g., Literal["yes", "no"]) to a step expecting a compatible primitive or union (e.g., str).

Root Cause

In _typing_utils.is_type_compatible():

  1. get_origin(Literal[...]) resolves to typing.Literal.
  2. When the target type was a primitive like str, target_origin was None.
  3. Because Literal was not handled in union or non-generic branches, execution fell through to Case 5 (source_origin != target_origin), incorrectly returning False.
  4. Similarly, when both source and target were Literal types (e.g., Literal["a"] -> Literal["a", "b"]), compatibility was not evaluated with subset semantics.

Changes

  • Updated is_type_compatible() in _typing_utils.py:
    • When both source and target are Literal, checks that source values are a subset of target values.
    • When source is Literal and target is a Union, verifies each literal value is compatible with at least one target branch.
    • When source is Literal and target is a general type, verifies all literal values' types are compatible with target.
    • When target is Literal but source is non-literal, rejects assignment.
  • Added test_type_compatibility_literals in tests/workflow/test_typing_utils.py.
  • Added end-to-end test_workflow_builder_literal_to_str_edge in tests/workflow/test_workflow_builder.py.

Validation

  • uv run --group dev pytest packages/core/tests/workflow/test_typing_utils.py (69 passed)
  • uv run --group dev pytest packages/core/tests/workflow/test_workflow_builder.py (20 passed)
  • ruff check and ruff format passed with 0 errors.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 18:54
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Oct 14, 2026
@github-actions github-actions Bot changed the title fix(workflows): support Literal types in workflow edge type compatibility Python: fix(workflows): support Literal types in workflow edge type compatibility Oct 14, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Literal comparison and source-union handling still produce incorrect compatibility results.

2 open findings
What changed in this PR

Addresses #9354 by adding Literal support to Python workflow edge type validation.

Changes:

  • Adds compatibility checks for Literal sources and targets.
  • Adds unit tests and a workflow-build regression test.
File Description
python/​packages/​core/​tests/​workflow/​test_workflow_builder.py Tests building a Literal-to-string edge.
python/​packages/​core/​tests/​workflow/​test_typing_utils.py Tests Literal compatibility cases.
python/​packages/​core/​agent_framework/​_workflows/​_typing_utils.py Implements Literal compatibility checks.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

# Case 1b: Literal type compatibility
if source_origin in (Literal, typing_extensions.Literal):
if target_origin in (Literal, typing_extensions.Literal):
return set(source_args).issubset(set(target_args))
Comment on lines +505 to +506
if target_origin in (Literal, typing_extensions.Literal):
return False
@nithin42
Nithin (nithin42) deleted the fix/workflow-literal-type-compatibility branch October 14, 2026 19:06

This branch was successfully deployed

1 active deployment
github-app-auth — b6d70605 Deployed Oct 14, 2026 by nithin42 via team_check #6816
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Workflow build rejects an edge from an executor that sends a Literal type to one that handles str

2 participants