Repository navigation
refactor(python/builder,cli)!: omit unset schema defaults in builders - #4566
jacobsimionato wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a breaking change to the PythonEmitter and generated Python builders. Optional fields with JSON Schema defaults are now emitted with default=None and have their schema defaults recorded in json_schema_extra instead of materializing unauthored defaults directly onto wire payloads. Consequently, numerous golden test files, documentation examples, and test cases have been updated to reflect the omission of these unauthored default properties from serialized payloads. I have no feedback to provide as there are no review comments.
…erated builders Signed-off-by: Jacob Simionato <jsimionato@google.com>
64020df to
0938376
Compare
📖 Docs Site Preview DeployedPreview URL: https://a2ui.org/pr-preview/pr-4566/ Modified pages in this PR: Built from commit |
Problem
Builders are the typed Python component classes (such as
TextorColumn) thatdart/a2ui_cligenerates from a catalog's JSON Schema.Until now, every optional property with a schema default was given that default as its Python value. So
Text(text="Hi")serialized as{"text": "Hi", "variant": "body"}, and everyColumncarried"justify": "start", "align": "stretch".Payloads grew with keys the author never wrote, and builder output did not match the hand-written core models in
a2ui.core.basic_catalogor messages written by hand or by an LLM. Macros (Python functions that expand into A2UI components on the server) had the same extra keys because they expand through these builders.Replaces #4532 (GitHub marked it merged during a stack reorder; nothing landed on main).
Stack: 3/11 (overview) · previous: #4565 · next: #4567
Reviewer guide: A2UI Python macros: reviewer guide explains how the whole stack fits together.
Summary
PythonEmitter, the Python code generator indart/a2ui_cli, now emits optional properties that have a schema default asField(default=None, ..., json_schema_extra={"default": <literal>}). Unset fields stayNone, and the existingexclude_none=Trueserialization leaves them out of the payload. Required properties are unchanged.Model.model_json_schema(), the same patterna2ui.core.basic_catalogalready uses.a2ui_agentanda2ui_clichangelogs.Breaking changes and migration
"variant": "body"must pass the value explicitly. An explicit value equal to the default is still serialized. A missing optional key means the schema default, and the bundled renderers fall back to the same values (for example'body'forText.variant), so rendered UI should not change.Noneinstead of the default, for exampleText(text="Hi").variantandSlider(max=10, value=5).min. Read the default from the schema instead. Thev0_9builders behave the same way:a2ui_cliwho generate their own Python catalogs get the same change when they regenerate.Where to review
python_emitter.dart)basic.py, 20 builder goldens, 30 macro goldens)codegen_test.dart, 2 Python test files,macros.yaml)Start here:
dart/a2ui_cli/lib/src/emitters/python/python_emitter.dart: the newhasSchemaDefaultflag and how it routes optional fields into theField(...)branch.python/a2ui_agent/src/a2ui/builder/v1_0/catalogs/basic.py: spot-check the regenerated fields, including aliased ones such asChoicePicker.display_style.python/a2ui_agent/tests/test_pydantic_builders_v1_0.py: the new test covers falsy defaults (Slider.min,ChoicePicker.filterable,DateTimeInput.enable_date).Implementation details
hasSchemaDefault = !prop.isRequired && defaultLiteral != null. A field with a schema default now always takes theField(...)path, even with no alias or bounds, so the generator also emitsfrom pydantic import Fieldfor such catalogs.name: Optional[T] = None) is used only for optional fields with no alias, constraints, or schema default.json_schema_extrais appended after alias and bound arguments, for exampleField(default=None, alias="maxRows", ge=1, json_schema_extra={"default": 10}).conformance/agent/macros/macros.yamldropsjustifyandalignfrom the expectedok_badge__row_1Rowin one macro case.test_bare_model_dump_keeps_children_nestedinpython/a2ui_agent/tests/test_pydantic_builders.pynow also checks the v0.9 builders for omitted, explicit, and schema-recorded defaults.Verification
Measured at the stack tip (all 11 commits applied), not at this commit alone:
a2ui_clitests pass (116), including the expandedPythonEmitterdefault test indart/a2ui_cli/test/codegen_test.dart.