Repository navigation
fix(python/macros)!: raise on macro errors, correct v1.0 schemas (#2748) - #4528
jacobsimionato wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces several breaking changes and bug fixes to the MacroExpander in a2ui_agent. Key updates include changing the default on_error behavior to "raise", rejecting duplicate macro names and bare ComponentRef root returns, and preventing @checks envelope propagation onto non-checkable components. Additionally, it resolves several v1.0 runtime bugs, such as preserving explicit None values, recursively stamping owning catalogIds onto nested function calls, and improving schema synthesis and coercion for Literal and Enum types. As there are no review comments, I have no feedback to provide.
2299e74 to
dbd63eb
Compare
📖 Docs Site Preview DeployedPreview URL: https://a2ui.org/pr-preview/pr-4528/ Modified pages in this PR: Built from commit |
…ise (#2748) Signed-off-by: Jacob Simionato <jsimionato@google.com>
dbd63eb to
740651b
Compare
Problem
Macros are Python functions that agents expose to the model as composite components.
MacroExpanderexpands them into standard A2UI components on the server before messages reach the client. Several bugs changed what the model or the client saw:nullfields were dropped from expanded messages.Literal[True, False]andIntEnumparameters got the wrong JSON Schema type in the schema shown to the model.Enumparameters reached the macro as raw strings.Tone | strproduced aoneOfschema that rejects valid values.Fixes #2748
Stack: 5/11 (overview) · previous: #4567 · next: #4529
Reviewer guide: A2UI Python macros: reviewer guide explains how the whole stack fits together.
Summary
Enumarguments arrive as members, registration is stricter, and model-facing schemas change. See the next section.Nonefields survive expansion, for exampleUpdateDataModel(value=None)andCreateSurface(catalogId=None).Annotatedparameter descriptions now win over docstrings.Child | DataBinding) becomes a component reference (ComponentRef).MacroExpandergains publicmacro_names,has_macro, andexpand_macrohelpers, so tooling and tests no longer reach into private internals.Breaking changes and migration
on_erroris"raise". Callers that relied on failed macros being left unexpanded must passMacroExpander(..., on_error="passthrough").Enumparameters arrive as members. Code that compared anEnumparameter to a raw string (tone == "info") must compare to the member, use.value, or useStrEnum. A value outside the declared members (includingTruefor anIntEnum) now fails expansion withA2uiValidationError, or is left unexpanded underon_error="passthrough".MacroExpander([...])raisesA2uiValidationErroron duplicate macro names andTypeErroron objects that are not@macrocallables (both were silently ignored before). A macro that returns a bareComponentRefas its root now fails expansion; wrap it in a concrete component.Dynamic*types (such asDynamicString) that accept a literal, a data binding, or a function call. Other union parameters now useanyOfinstead ofoneOf, and a union such asstr | DataBinding(no function call option) no longer widens toDynamicString. This changes the catalog text in prompts; no code change is needed, but regenerate any schema snapshot tests.New public API
Where to review
Start here:
python/a2ui_agent/src/a2ui/transformers/macros/expander.py: the newon_errordefault, duplicate-name checks in__init__, andexpand_macro, which always raises on nested failures.python/a2ui_agent/src/a2ui/transformers/macros/processor.py: the enum coercion helpers and the union handling in_coerce_value. Check thatboolis never accepted as anIntEnum, and when a raw wire value may skip enum coercion.python/a2ui_agent/src/a2ui/transformers/macros/macro.py:_enum_values_to_schema, the stricterDynamic*condition, andanyOfdeduplication in_map_type_hint_to_schema.Implementation details
expander.pyto_message_dictsinstead ofmodel_dump(exclude_none=True), which is what keeps explicitNonefields. It readsversionfrom the model attribute first, sinceto_message_dictsonly emits fields that were set._ExpansionContextgains astrictflag.expand_macrosets it so nested macro failures raise even when the expander useson_error="passthrough";_handle_expansion_errorhonors it.expand_macrochecks the requested protocol version against the expander's target family, raisesA2uiValidationErrorfor unknown names, and wraps unexpected exceptions inA2uiValidationError(andRecursionErrorinA2uiRecursionError).macro.py_enum_values_to_schemabuildsenumschemas for bothLiteralandEnum, unwrappingLiteral[EnumMember, ...]to member values and raising on an empty value list.integerbranch is dropped when anumberbranch exists, and a single remaining branch is emitted withoutanyOf.ChildandChildListshortcuts inside theDynamic*path were removed; those unions fall through to the existing child-slot handling or toanyOf.processor.py_enum_branches,_literal_enum_members,_coerce_enum_value, and_union_permits_raw_valuedecide whether a wire value becomes anEnummember, passes through a non-enum branch, or is rejected. Members of a differentEnumare never converted, even if they compare equal._should_coerce_str_to_component_refturns a string into aComponentReffor unions with a child-slot branch, unless another branch accepts raw strings.ValueError, so an invalid enum inside one model branch lets the next branch try.python/a2ui_agent/tests/conformance/macro_suite.pydefaultson_errorto"raise", matching the new expander default. Themacros.yamlpassthrough case already seton_error: "passthrough", so no YAML change was needed.docs/site/src/content/docs/guides/macros.mdxandpython/a2ui_agent/src/a2ui/transformers/macros/README.mddescribe the new default, enum coercion, andanyOfschemas;conformance/agent/macros/README.mdandconformance/conformance_harness_implementation_guide.mdlist"raise"as theon_errordefault;python/a2ui_agent/CHANGELOG.mdhas a breaking-change entry and a fix entry.Verification
Measured at the stack tip (all 11 commits applied), not on this commit alone:
This PR adds five tests to
python/a2ui_agent/tests/test_macros.py(Nonepreservation, registration rejection, enum and union schemas and coercion, the public helpers, and enums from other classes) and updates existing tests for the new default andanyOfschemas.