Skip to content

refactor(python/builder,cli)!: omit unset schema defaults in builders - #4566

Open
jacobsimionato wants to merge 1 commit into
macros/4-typed-function-calls-v2from
macros/5-omit-builder-defaults-v2
Open

jacobsimionato wants to merge 1 commit into
macros/4-typed-function-calls-v2from
macros/5-omit-builder-defaults-v2

Conversation

@jacobsimionato

@jacobsimionato jacobsimionato commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Builders are the typed Python component classes (such as Text or Column) that dart/a2ui_cli generates 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 every Column carried "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_catalog or 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

  1. PythonEmitter, the Python code generator in dart/a2ui_cli, now emits optional properties that have a schema default as Field(default=None, ..., json_schema_extra={"default": <literal>}). Unset fields stay None, and the existing exclude_none=True serialization leaves them out of the payload. Required properties are unchanged.
  2. The schema default is still visible through Model.model_json_schema(), the same pattern a2ui.core.basic_catalog already uses.
  3. Regenerated the v0.9 and v1.0 basic catalog builders and the builder and macro conformance expected-output files that contained the old defaults.
  4. Updated the builder README example, the macros guide output samples, and the a2ui_agent and a2ui_cli changelogs.

Breaking changes and migration

  1. Serialized builder and macro output no longer contains optional schema defaults unless the caller passes them. Code or tests that expect keys such as "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' for Text.variant), so rendered UI should not change.
  2. Reading an unset optional attribute now returns None instead of the default, for example Text(text="Hi").variant and Slider(max=10, value=5).min. Read the default from the schema instead. The v0_9 builders behave the same way:
from a2ui.builder.v1_0 import Text

assert Text(text="Hi").variant is None
assert Text.model_json_schema()["properties"]["variant"]["default"] == "body"
  1. Users of a2ui_cli who generate their own Python catalogs get the same change when they regenerate.

Where to review

Category Files Diff (+/-)
Production (python_emitter.dart) 1 +5 / -3
Generated (2 basic.py, 20 builder goldens, 30 macro goldens) 52 +187 / -264
Tests (codegen_test.dart, 2 Python test files, macros.yaml) 4 +143 / -6
Docs and changelogs 4 +27 / -16
Total 61 +362 / -289

Start here:

  1. dart/a2ui_cli/lib/src/emitters/python/python_emitter.dart: the new hasSchemaDefault flag and how it routes optional fields into the Field(...) branch.
  2. python/a2ui_agent/src/a2ui/builder/v1_0/catalogs/basic.py: spot-check the regenerated fields, including aliased ones such as ChoicePicker.display_style.
  3. 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
  1. hasSchemaDefault = !prop.isRequired && defaultLiteral != null. A field with a schema default now always takes the Field(...) path, even with no alias or bounds, so the generator also emits from pydantic import Field for such catalogs.
  2. The simple path (name: Optional[T] = None) is used only for optional fields with no alias, constraints, or schema default.
  3. json_schema_extra is appended after alias and bound arguments, for example Field(default=None, alias="maxRows", ge=1, json_schema_extra={"default": 10}).
  4. conformance/agent/macros/macros.yaml drops justify and align from the expected ok_badge__row_1 Row in one macro case.
  5. test_bare_model_dump_keeps_children_nested in python/a2ui_agent/tests/test_pydantic_builders.py now 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:

  1. Full Python suite passes (6027 passed).
  2. mypy clean (283 source files).
  3. 56 conformance goldens valid.
  4. Dart a2ui_cli tests pass (116), including the expanded PythonEmitter default test in dart/a2ui_cli/test/codegen_test.dart.
  5. Prettier and pyink clean on every commit; every commit has a DCO sign-off.

@jacobsimionato jacobsimionato changed the title refactor(python/builder,cli)!: omit unauthored schema defaults in generated builders refactor(python/builder,cli)!: omit unset schema defaults in builders Dec 3, 2026

@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 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>
@jacobsimionato
jacobsimionato force-pushed the macros/5-omit-builder-defaults-v2 branch from 64020df to 0938376 Compare December 3, 2026 12:24
@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-4566/

Modified pages in this PR:

Built from commit 0938376 · 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

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.

1 participant