Skip to content

fix(python/macros)!: raise on macro errors, correct v1.0 schemas (#2748) - #4528

Open
jacobsimionato wants to merge 1 commit into
macros/5b-stateless-v10-macros-v2from
macros/1-runtime-bug-fixes
Open

jacobsimionato wants to merge 1 commit into
macros/5b-stateless-v10-macros-v2from
macros/1-runtime-bug-fixes

Conversation

@jacobsimionato

@jacobsimionato jacobsimionato commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Macros are Python functions that agents expose to the model as composite components. MacroExpander expands them into standard A2UI components on the server before messages reach the client. Several bugs changed what the model or the client saw:

  1. By default, a macro that failed at runtime was logged and left unexpanded. The client received a component name it cannot render, and the agent code calling the expander never saw an error.
  2. Explicit null fields were dropped from expanded messages.
  3. Literal[True, False] and IntEnum parameters got the wrong JSON Schema type in the schema shown to the model.
  4. Enum parameters reached the macro as raw strings.
  5. Overlapping unions such as Tone | str produced a oneOf schema 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

  1. Breaking: failed macros raise by default, Enum arguments arrive as members, registration is stricter, and model-facing schemas change. See the next section.
  2. Explicitly set None fields survive expansion, for example UpdateDataModel(value=None) and CreateSurface(catalogId=None).
  3. Annotated parameter descriptions now win over docstrings.
  4. A string passed to a parameter typed "child component or data binding" (Child | DataBinding) becomes a component reference (ComponentRef).
  5. MacroExpander gains public macro_names, has_macro, and expand_macro helpers, so tooling and tests no longer reach into private internals.

Breaking changes and migration

  1. Default on_error is "raise". Callers that relied on failed macros being left unexpanded must pass MacroExpander(..., on_error="passthrough").
  2. Enum parameters arrive as members. Code that compared an Enum parameter to a raw string (tone == "info") must compare to the member, use .value, or use StrEnum. A value outside the declared members (including True for an IntEnum) now fails expansion with A2uiValidationError, or is left unexpanded under on_error="passthrough".
  3. Stricter registration. MacroExpander([...]) raises A2uiValidationError on duplicate macro names and TypeError on objects that are not @macro callables (both were silently ignored before). A macro that returns a bare ComponentRef as its root now fails expansion; wrap it in a concrete component.
  4. Model-facing catalog schemas change. A2UI v1.0 defines Dynamic* types (such as DynamicString) that accept a literal, a data binding, or a function call. Other union parameters now use anyOf instead of oneOf, and a union such as str | DataBinding (no function call option) no longer widens to DynamicString. This changes the catalog text in prompts; no code change is needed, but regenerate any schema snapshot tests.

New public API

from a2ui.builder import v1_0 as builder_v1_0
from a2ui.transformers.macros import MacroExpander, macro

@macro
def StatusBadge(label: str) -> builder_v1_0.Text:
    return builder_v1_0.Text(text=label)

expander = MacroExpander([StatusBadge])  # on_error="raise" by default
assert expander.macro_names == ("StatusBadge",)
assert expander.has_macro("StatusBadge")
# Returns a flat list[dict] of expanded components. instance_id is the root
# component ID used to namespace children; protocol_version is optional.
components = expander.expand_macro("StatusBadge", {"label": "Live"}, instance_id="b1")

Where to review

Category Files Diff (+/-)
Production 3 +337 / -48
Tests 2 +495 / -9
Docs and changelog 5 +48 / -20
Total 10 +880 / -77

Start here:

  1. python/a2ui_agent/src/a2ui/transformers/macros/expander.py: the new on_error default, duplicate-name checks in __init__, and expand_macro, which always raises on nested failures.
  2. python/a2ui_agent/src/a2ui/transformers/macros/processor.py: the enum coercion helpers and the union handling in _coerce_value. Check that bool is never accepted as an IntEnum, and when a raw wire value may skip enum coercion.
  3. python/a2ui_agent/src/a2ui/transformers/macros/macro.py: _enum_values_to_schema, the stricter Dynamic* condition, and anyOf deduplication in _map_type_hint_to_schema.
Implementation details
  • expander.py
    • Message preparation converts messages with to_message_dicts instead of model_dump(exclude_none=True), which is what keeps explicit None fields. It reads version from the model attribute first, since to_message_dicts only emits fields that were set.
    • _ExpansionContext gains a strict flag. expand_macro sets it so nested macro failures raise even when the expander uses on_error="passthrough"; _handle_expansion_error honors it.
    • expand_macro checks the requested protocol version against the expander's target family, raises A2uiValidationError for unknown names, and wraps unexpected exceptions in A2uiValidationError (and RecursionError in A2uiRecursionError).
  • macro.py
    • _enum_values_to_schema builds enum schemas for both Literal and Enum, unwrapping Literal[EnumMember, ...] to member values and raising on an empty value list.
    • Union branches are deduplicated by their JSON form; an integer branch is dropped when a number branch exists, and a single remaining branch is emitted without anyOf.
    • The Child and ChildList shortcuts inside the Dynamic* path were removed; those unions fall through to the existing child-slot handling or to anyOf.
  • processor.py
    • _enum_branches, _literal_enum_members, _coerce_enum_value, and _union_permits_raw_value decide whether a wire value becomes an Enum member, passes through a non-enum branch, or is rejected. Members of a different Enum are never converted, even if they compare equal.
    • _should_coerce_str_to_component_ref turns a string into a ComponentRef for unions with a child-slot branch, unless another branch accepts raw strings.
    • Structured model union coercion now also catches ValueError, so an invalid enum inside one model branch lets the next branch try.
  • Conformance: python/a2ui_agent/tests/conformance/macro_suite.py defaults on_error to "raise", matching the new expander default. The macros.yaml passthrough case already set on_error: "passthrough", so no YAML change was needed.
  • Docs (5 files): docs/site/src/content/docs/guides/macros.mdx and python/a2ui_agent/src/a2ui/transformers/macros/README.md describe the new default, enum coercion, and anyOf schemas; conformance/agent/macros/README.md and conformance/conformance_harness_implementation_guide.md list "raise" as the on_error default; python/a2ui_agent/CHANGELOG.md has a breaking-change entry and a fix entry.
Verification

Measured at the stack tip (all 11 commits applied), not on this commit alone:

  1. Full Python suite passes (6027 passed).
  2. mypy is clean (283 source files).
  3. 56 conformance goldens are valid.
  4. Prettier and pyink are clean on every commit, and every commit has a DCO sign-off.

This PR adds five tests to python/a2ui_agent/tests/test_macros.py (None preservation, registration rejection, enum and union schemas and coercion, the public helpers, and enums from other classes) and updates existing tests for the new default and anyOf schemas.

@gemini-code-assist gemini-code-assist Bot 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.

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.

@jacobsimionato jacobsimionato changed the title fix(python/macros)!: fix v1.0 runtime bugs and default on_error to raise (#2748) fix(python/macros)!: fix v1.0 bugs and default on_error to raise (#2748) Dec 2, 2026
@jacobsimionato
jacobsimionato removed this pull request from stack #4536 December 2, 2026 23:18
@jacobsimionato
jacobsimionato removed this pull request from stack #4552 December 3, 2026 12:03
@jacobsimionato
jacobsimionato force-pushed the macros/1-runtime-bug-fixes branch from 2299e74 to dbd63eb Compare December 3, 2026 12:04
@jacobsimionato
jacobsimionato marked this pull request as ready for review December 3, 2026 12:04
@jacobsimionato
jacobsimionato changed the base branch from stack/macros-async-base to main December 3, 2026 12:06
@jacobsimionato
jacobsimionato removed this pull request from stack #4563 December 3, 2026 12:06
@jacobsimionato
jacobsimionato changed the base branch from main to macros/5b-stateless-v10-macros-v2 December 3, 2026 12:07
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs Site Preview Deployed

Preview URL: https://a2ui.org/pr-preview/pr-4528/

Modified pages in this PR:

Built from commit 740651b · Workflow run · GitHub Pages may take ~30–60s to propagate updates. This preview is removed automatically when the PR closes.

github-actions Bot added a commit that referenced this pull request Dec 3, 2026
@jacobsimionato jacobsimionato changed the title fix(python/macros)!: fix v1.0 bugs and default on_error to raise (#2748) fix(python/macros)!: raise on macro errors, correct v1.0 schemas (#2748) Dec 3, 2026
…ise (#2748)

Signed-off-by: Jacob Simionato <jsimionato@google.com>
@jacobsimionato
jacobsimionato force-pushed the macros/1-runtime-bug-fixes branch from dbd63eb to 740651b Compare December 3, 2026 12:24
github-actions Bot added a commit that referenced this pull request Dec 3, 2026

This branch has not been deployed

No deployments
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.

macros: MacroParser silently swallows expansion errors and emits the unexpanded macro component

1 participant