Repository navigation
fix(python/builder,cli)!: fix builder ID collisions and CLI schema parity - #4564
Open
jacobsimionato wants to merge 1 commit into
Open
jacobsimionato wants to merge 1 commit into
jacobsimionato wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request aligns the Python code generator with catalog JSON schemas by supporting integer, numeric, boolean, and nullable enums, merging allOf constraints, and resolving oneOf/anyOf nullability. It tightens validation in generated builders by emitting strict Dynamic* aliases and validation bounds (such as ge, le, min_length, and max_length). Additionally, it enforces stricter builder ID collision checks during component tree flattening, preserves explicit None values on required nullable fields, and introduces a catalog registry (CATALOG_COMPONENTS) and CATALOG_ID to generated modules. I have no feedback to provide as there are no review comments.
…ound-trip guardrails Signed-off-by: Jacob Simionato <jsimionato@google.com>
jacobsimionato
force-pushed
the
macros/3-builder-ids-and-cli-v2
branch
from
December 3, 2026 12:24
69fae65 to
bf834b7
Compare
This branch has not been 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.
Problem
Python builders are typed component classes that
dart/a2ui_cligenerates from a catalog. The flattener turns a tree of builders into the flat, ID-linked component list that A2UI sends to a surface (one rendered UI on the client). This PR fixes two problems with them:[Text(...), Text(...)]gave both roots the IDroot__text_1. A macro (a Python function that expands into A2UI components on the server) could also pick an auto ID that was already used on the surface.FunctionCallonIcon.name, afloatwhere the schema says integer, or an emptyTabs.tabs.Related: #4189, #4190. Replaces #4530 (GitHub marked it merged during a stack reorder; nothing landed on main).
Stack: 1/11 (overview) · previous: none (base is
main) · next: #4565Reviewer guide: A2UI Python macros: reviewer guide explains how the whole stack fits together.
Summary
ComponentReftargets, callerreserved_ids), then allocates automatic IDs around them. In a list, each anonymous root gets the first freeroot_<n>(counting from its index), and its anonymous descendants get<root>__<type>_<k>, such asroot_1__text_2. Real collisions raiseA2uiValidationError.ComponentTreegainsdangling_componentsandcatalog_id, and allowsroot=None.dart/a2ui_cligenerates builders that match the catalog schemas. Thev0_9andv1_0basic catalogs are regenerated.Breaking changes and migration
flatten_component_treeraisesA2uiValidationErrorfor duplicate explicit sibling IDs and for explicit IDs that matchreserved_idsor aComponentRef. Give sibling nodes distinct IDs.flatten_component_tree([Text(...), Text(...)])now emitsroot_0,root_1instead ofroot__text_1twice.ComponentTree.flatten()keeps explicit IDs verbatim.Card(id="hero", child=Text(id="title", ...))now emitstitle, nothero__title. AComponentTreeis a whole wire tree, so its IDs must round-trip.flatten_component_treekeeps its old prefixing default for subtrees such as macro output; passpreserve_ids=Trueto opt out.null. A required field set toNonenow appears as JSONnullinstead of being dropped byexclude_none=True.ComponentTree.rootis optional (ComponentBuilderNode | None = None). Checktree.root is not Nonebefore reading it.Dynamic*fields (literal, data binding, or function call) use Pydantic'sStrictStr/StrictInt/StrictFloat, so anintpassed toText.textis rejected instead of coerced.Icon.namedropsFunctionCall.Slider.stepsisint >= 1,Lengthmin/maxareint,Tabs.tabsneeds at least one item, andDateTimeInput.min/maxareDynamicString(previouslyAny).a2ui_cliDart API.EnumType.valueswidens fromList<String>toList<Object>, and the sealedTypeDescriptorgainsFunctionCallType. Exhaustiveswitchexpressions onTypeDescriptormust handle it.New public API
dangling_componentsholds subtrees that aren't attached torootbut go out in the same flattened list.catalog_idrecords which catalog the tree targets, next tosurface_id;flatten()doesn't read it.Also new:
preserve_idsonflatten_component_treeandIdAllocator,external_idsonIdAllocator,CATALOG_COMPONENTS(component name to builder class) in generated catalogs, and__all__ina2ui.builder.v0_9.models.Where to review
Start here:
python/a2ui_agent/src/a2ui/builder/core/flattener.py: how IDs are reserved across all roots before any automatic ID is picked.python/a2ui_agent/src/a2ui/builder/core/child.pyandid_allocator.py: how a collision found deep in the tree reaches the caller as an error.python/a2ui_agent/src/a2ui/builder/core/base_model.py: keeping requiredNonefields on the wire without breakinginclude/exclude.dart/a2ui_cli/lib/src/analyzer/catalog_analyzer.dart: how schema constraints,allOf, and nullability turn into Python types.Implementation details
flatten_component_treeandComponentTree.flatten()both call the shared two-pass_flatten_root_entries. Pass 1 reserves IDs; pass 2 allocates and emits._collect_unscoped_idsruns a throwaway scan to find IDs that land on the wire unprefixed (ComponentRefIDs, plus explicit IDs whenpreserve_idsis set), so anonymous roots avoid them when choosingroot_<n>.reserved,external_ids,seen_explicit, andvisitedacross all roots. Pass 2 uses one baseIdAllocatorand gives each root a scoped view through_with_scope, which shares the claimed and reserved sets.IdAllocator._check_unclaimedrejects an explicit ID that resolves to an external ID or one already claimed in the tree.FlattenContext.errorand re-raised by the flattener, because Pydantic's union serializer swallows exceptions raised inside theChildPlainSerializer.ComponentTree.flatten()builds entries for the root and each dangling component and calls_flatten_root_entries(..., preserve_ids=True). Dangling components without an ID share the root's scope.BuilderBaseModeladds a wrapmodel_serializerthat puts requiredNonefields back afterexclude_none=True, while honoringinclude/exclude/exclude_unset.transformers/macros/expander.py,processor.py):_expand_component_listcollects the IDs in the incoming list plus any callerreserved_ids, passes them (minus the macro's own ID) down toflatten_component_tree, and adds each expansion's output IDs before expanding the next macro.catalog_analyzer.dart:"integer"maps toPrimitiveKind.integer(previously grouped with"number"). Readsminimum/maximum/minItems/maxItems, mergesallOf(tightest bound wins), resolvesFunctionCall$refinoneOf/anyOf, and tracks nullability onPropertyDescriptor.type_mapper.dart:DynamicTypeover string, number, boolean, any, and string list maps toDynamicString,DynamicNumber,DynamicBoolean,DynamicValue, andDynamicStringList.python_emitter.dart: emitsField(ge=..., le=..., min_length=..., max_length=...), theCATALOG_IDandCATALOG_COMPONENTSregistry, the extra runtime imports, and the full__all__.python/a2ui_agent/src/a2ui/builder/README.mdupdates theIcon.nametype and the note on custom serializers.Verification
Measured at the stack tip (all 11 commits applied); these runs were not repeated on this commit alone:
a2ui_clitests pass (116).Tests added in this PR:
test_multi_root_flattening_and_collision_avoidance,test_component_tree_roundtrip_and_dag_order_independence, andtest_required_nullable_serialization_and_v09_basic_catalog_parityintest_pydantic_builders.py,test_v10_basic_catalog_constraints_registry_and_strict_dynamic_typesintest_pydantic_builders_v1_0.py,test_macro_expander_reserves_existing_sibling_idsintest_macros.py, and a schema-parity case indart/a2ui_cli/test/codegen_test.dart.