Repository navigation
Python: fix(workflows): support Literal types in workflow edge type compatibility - #9396
Closed
Nithin (nithin42) wants to merge 1 commit into
Closed
Nithin (nithin42) wants to merge 1 commit into
Nithin (nithin42) wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟡 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 |
This branch was successfully deployed
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.

Summary
Fixes #9354.
Resolves an issue where
WorkflowBuilder.build()rejected edges from an executor outputting aLiteraltype (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():get_origin(Literal[...])resolves totyping.Literal.str,target_originwasNone.Literalwas not handled in union or non-generic branches, execution fell through to Case 5 (source_origin != target_origin), incorrectly returningFalse.Literaltypes (e.g.,Literal["a"]->Literal["a", "b"]), compatibility was not evaluated with subset semantics.Changes
is_type_compatible()in_typing_utils.py:Literal, checks that source values are a subset of target values.Literaland target is aUnion, verifies each literal value is compatible with at least one target branch.Literaland target is a general type, verifies all literal values' types are compatible with target.Literalbut source is non-literal, rejects assignment.test_type_compatibility_literalsintests/workflow/test_typing_utils.py.test_workflow_builder_literal_to_str_edgeintests/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 checkandruff formatpassed with 0 errors.