Skip to content

API Reference parrot_formdesigner.api.a2ui_wire

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_formdesigner.api.a2ui_wire

View source

A2UI wire helpers for the form endpoints (FEAT-544, TASK-3074).

Spec: sdd/specs/a2ui-form-output-renderer.spec.md, §3 Module 4.

Pure helper module the form endpoints (FormAPIHandler.submit_data / .validate, TASK-3075/3076) call to detect an inbound A2UI v1.0 renderer->agent action envelope, unwrap it into field_id-keyed answers, and build the outbound A2UI replies. Framing mirrors A2UIHandler (ai-parrot-server/handlers/a2ui.py): a single envelope is the response body; several are wrapped as {"messages": [...]}.

ai-parrot (and therefore parrot.outputs.a2ui) is an OPTIONAL dependency of parrot-formdesigner — every parrot.* symbol is imported lazily, inside :func:_a2ui_ns. :func:is_a2ui_request in particular must NEVER raise: it returns False (the legacy JSON path) whenever parrot.outputs.a2ui is not importable.

Class A2UIActionSubmission

Answers unwrapped from an inbound A2UI action envelope.

Fields:

Field Type Default Description
surface_id str The surface the action originated from.
action_name str "form.submit" or "form.validate".
source_component_id str The component that triggered the event.
answers dict[str, Any] field_id-keyed answers (JSON-Pointer tokens unescaped).
raw_context dict[str, Any] The action's raw context dict, for callers that need something beyond answers (e.g. form_uid).

Class A2UIWireError

Raised when an inbound A2UI envelope cannot be unwrapped.

Parameter Type Default Description
status int
envelope dict[str, Any]

Attributes:

Attribute Type Description
status The HTTP status the caller should reply with.
envelope A ready-to-serialize generic A2UI error envelope ({"version": "v1.0", "error": {...}}).

Attribute A2UI_CANCEL_ACTION

Attribute A2UI_MAX_BODY_BYTES

Attribute A2UI_MAX_SCHEMA_DEPTH

Attribute A2UI_SUBMIT_ACTION

Attribute A2UI_VALIDATE_ACTION

Alias FormSchema

Re-exported from parrot_formdesigner.core.schema.FormSchema.

Function a2ui_response

Frame one or more A2UI envelopes into an aiohttp response.

A single envelope is returned as the body with Content-Type: application/a2ui+json; several (or zero) are wrapped as {"messages": [...]} with Content-Type: application/json (mirrors A2UIHandler's framing).

a2ui_response(envelopes: list[dict[str, Any]], *, status: int) -> web.Response

Parameter Type Default Description
envelopes list[dict[str, Any]] The envelopes to send (each already serialize()d).
status int The HTTP status code for the response.

Returns: web.Response — The framed web.Response.

Function apply_body_cap

Lower this request's aiohttp read cap, and return the cap in force.

aiohttp enforces BaseRequest._client_max_size inside BaseRequest.read() -- and therefore inside .text() and .json() -- raising :exc:aiohttp.web.HTTPRequestEntityTooLarge as soon as the bytes accumulated so far exceed it. Because it counts bytes as they arrive instead of trusting Content-Length, it is the one seam that also covers a chunked body, where content_length is None -- the bypass FEAT-822 exists to close (issue:bf978b6bae8a).

Call this immediately before await request.json() and catch web.HTTPRequestEntityTooLarge at the call site to shape the 413; :func:oversized_response picks the right shape.

The cap is only ever lowered, never raised, so a host application that already configures a smaller client_max_size keeps it.

apply_body_cap(request: web.Request, *, limit: int | None = None) -> int

Parameter Type Default Description
request web.Request The incoming aiohttp request.
limit int | None None Byte cap; None resolves to :data:A2UI_MAX_BODY_BYTES at call time, so the module attribute stays monkeypatchable.

Returns: int — The cap now in force for this request, to echo as max_bytes.

Function confirmation

Build the A2UI reply for a 200 (submission summary + status update).

confirmation(surface_id: str, result: dict[str, Any], *, message: str = 'Form submitted.') -> list[dict[str, Any]]

Parameter Type Default Description
surface_id str The surface to address the confirmation to.
result dict[str, Any] The legacy submit result dict — only submission_id, is_valid, forwarded, and forward_status are echoed.
message str 'Form submitted.' The confirmation text for the root-status Text.

Returns: list[dict[str, Any]] — An updateDataModel{path: "/submission"} envelope, followed by an updateComponents envelope replacing root-status.

Function exceeds_json_depth

Return whether a decoded JSON value nests deeper than max_depth.

Counts container nesting levels: a flat object or array is depth 1. The walk uses an explicit stack rather than recursion, so a hostile payload cannot exhaust the Python stack while it is being validated (FEAT-822 / issue:bf978b6bae8a item S3), and it short-circuits on the first node past the cap rather than traversing the whole structure.

exceeds_json_depth(value: Any, *, max_depth: int | None = None) -> bool

Parameter Type Default Description
value Any Any JSON-decoded value.
max_depth int | None None Depth cap; None resolves to :data:A2UI_MAX_SCHEMA_DEPTH at call time.

Returns: bool — True when some container sits deeper than max_depth.

Alias field_id_from_pointer_token

Re-exported from parrot_formdesigner.renderers.a2ui.field_id_from_pointer_token.

Alias field_pointer

Re-exported from parrot_formdesigner.renderers.a2ui.field_pointer.

Function is_a2ui_content_type

Return whether the request declares the A2UI media type.

Headers-only counterpart to :func:is_a2ui_request, for the oversize branch, which must choose an error shape BEFORE the body is parsed. Deliberately has no parrot.* import so it works without the optional A2UI extra installed. Never raises.

is_a2ui_content_type(request: web.Request) -> bool

Parameter Type Default Description
request web.Request The incoming aiohttp request.

Returns: bool — True when the request's media type is application/a2ui+json.

Function is_a2ui_request

Detect whether an inbound request is speaking the A2UI wire.

True when Content-Type: application/a2ui+json, OR the JSON body's top level is {"version": "v1.0", "action": {...}}. Anything else (including ai-parrot being unavailable) is the legacy JSON path.

is_a2ui_request(request: web.Request, body: Any) -> bool

Parameter Type Default Description
request web.Request The incoming aiohttp request.
body Any The already-parsed JSON body (dict for the legacy path).

Returns: bool — True if this is an A2UI request, else False. Never raises.

Function is_a2ui_surface_body

Return whether a request body is an A2UI createSurface envelope.

Content-Type: application/a2ui+json is sufficient to select the surface import path. JSON requests also select it when their top-level object is a v1.0 createSurface envelope. This helper deliberately has no parrot.* imports so detection works without the optional A2UI extra installed.

is_a2ui_surface_body(request: web.Request, body: Any) -> bool

Parameter Type Default Description
request web.Request
body Any

Returns: bool

Function oversized_a2ui_response

Return the A2UI-enveloped 413 used by A2UI routes.

oversized_a2ui_response(surface_id: str, message: str = 'body too large') -> web.Response

Parameter Type Default Description
surface_id str The surface the error belongs to.
message str 'body too large' The human-readable error message.

Returns: web.Response — A 413 web.Response carrying a single A2UI error envelope.

Function oversized_json_response

Return the plain-JSON 413 used by non-A2UI routes.

oversized_json_response(limit: int) -> web.Response

Parameter Type Default Description
limit int The cap that was exceeded, echoed as max_bytes.

Returns: web.Response — A 413 web.Response whose shape matches the guards this replaces.

Function oversized_response

Return the 413 shaped for this request's wire. Never raises.

The oversize branch has to choose its error shape BEFORE the body is available, so the decision is made on the request headers alone.

oversized_response(request: web.Request, *, limit: int, surface_id: str | None = None, message: str = 'body too large') -> web.Response

Parameter Type Default Description
request web.Request The incoming aiohttp request.
limit int The cap that was exceeded, echoed as max_bytes on the plain-JSON shape.
surface_id str | None None The surface to name in the A2UI envelope; None on routes that have no A2UI shape at all.
message str 'body too large' The human-readable message for the A2UI envelope.

Returns: web.Response — An A2UI-enveloped 413 for an A2UI caller, else the plain-JSON 413. Falls back to the plain shape when the optional parrot.outputs.a2ui extra is not installed.

Function unwrap_action

Unwrap an inbound A2UI action envelope into field_id-keyed answers.

unwrap_action(form: FormSchema, body: dict[str, Any]) -> A2UIActionSubmission

Parameter Type Default Description
form FormSchema The form the envelope is submitted against (its form_uid determines the expected surfaceId).
body dict[str, Any] The parsed request body (an A2UI v1.0 envelope dict).

Returns: A2UIActionSubmission — The unwrapped :class:A2UIActionSubmission.

Raises:

  • A2UIWireError — (status 400) on a malformed envelope, a missing/ wrong-named action, a surfaceId mismatch, or non-dict answers.

Function validation_errors

Build the A2UI reply for a 422 (per-field errors + a data-model update).

validation_errors(surface_id: str, errors: dict[str, list[str] | str]) -> list[dict[str, Any]]

Parameter Type Default Description
surface_id str The surface to address the errors to.
errors dict[str, list[str] | str] field_id -> message(s). "__unknown__" (unknown-field rejections) and "_metadata" address path == "/answers".

Returns: list[dict[str, Any]] — One error{code: "VALIDATION_FAILED"} envelope per field, plus a trailing updateDataModel{path: "/errors"} envelope.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally