- Initial form rendering - Present a form as the first interaction before users start a conversation with your agent
- Dynamic form requests - Request forms at any point during a multi-turn conversation when your agent needs specific structured input
Initial Form Rendering
For initial form rendering, you specify the form structure when injecting the extension and then parse the response using a Pydantic model. The form is presented to users before they start a conversation with your agent.1
Import the form extension
Import
FormServiceExtensionServer, FormServiceExtensionSpec, FormRender, and field types from the Agent Stack SDK.2
Define your form data model
Create a Pydantic model with fields matching your form field IDs.
3
Add form parameter to your agent
Inject the form extension into your agent function using
FormServiceExtensionSpec.demand(initial_form=FormRender(...)).4
Parse form data
Call
form.parse_initial_form(model=YourModel) to extract the submitted form data.Dynamic Form Requests
For dynamic form requests during conversation, you can request forms at any point when your agent needs structured input. This is useful when your agent needs to collect additional information based on the conversation flow.1
Import the request form extension
Import
FormRequestExtensionServer, FormRequestExtensionSpec, FormRender, and field types from the Agent Stack SDK.2
Define your form data model
Create a Pydantic model with fields matching your form field IDs.
3
Add request form parameter to your agent
Inject the request form extension into your agent function using
FormRequestExtensionSpec().4
Request form dynamically
Call
await form_request.request_form(form=FormRender(...), model=YourModel) when you need to collect structured input.How to work with forms
Here’s what you need to know to add form capabilities to your agent: Import the form components:- For form fields and
FormRender, import fromagentstack_sdk.a2a.extensions - For initial forms, import
FormServiceExtensionServerandFormServiceExtensionSpecfromagentstack_sdk.a2a.extensions - For dynamic forms, import
FormRequestExtensionServerandFormRequestExtensionSpecfromagentstack_sdk.a2a.extensions
Annotated type hint.
For initial forms: Use FormServiceExtensionSpec.demand(initial_form=FormRender(...)) to specify the form structure and call form.parse_initial_form(model=YourModel) to extract data.
For dynamic forms: Use FormRequestExtensionSpec() and call await form_request.request_form(form=FormRender(...), model=YourModel) when needed.
Access form data: The recommended approach is to use a Pydantic model (or TypedDict, dataclass, or any class supported by pydantic.TypeAdapter) to load form data. Define a model with fields matching your form field IDs:
model=ContactInfo to parse_initial_form(...) or request_form(...) to get the form data directly as an instance of ContactInfo:
FormResponse which has a values dictionary. You can access values using form_data.values['field_id'].value. Different field types return different value types:
Form Field Types
The Agent Stack supports various field types for collecting different kinds of structured data:TextField
Basic text input fields for collecting strings, names, descriptions, etc.DateField
Date input fields for collecting dates and timestamps.FileField
File upload fields for collecting files from users.SingleSelectField
Single-select dropdown fields for choosing single option from a list.MultiSelectField
Multi-select dropdown fields for choosing multiple options from a list.CheckboxField
Single checkbox fields for boolean values.Form Layout Configuration
Control how your form appears using theFormRender configuration: