Skip to content

Add defaults kwarg to Form.from_model for runtime prefill#421

Merged
jlowin merged 2 commits intomainfrom
form-from-model-defaults
Apr 14, 2026
Merged

Add defaults kwarg to Form.from_model for runtime prefill#421
jlowin merged 2 commits intomainfrom
form-from-model-defaults

Conversation

@jlowin
Copy link
Copy Markdown
Member

@jlowin jlowin commented Apr 14, 2026

Field(default=...) bakes values into the model class — every render sees the same defaults. That's wrong for the common MCP case where an LLM has collected most of a form's values already and wants the user to edit a pre-filled form rather than a blank one (see PrefectHQ/fastmcp#3930). Form.from_model() now takes a defaults dict that overlays onto the model's class-level defaults at render time.

class BugReport(BaseModel):
    title: str
    severity: Literal["low", "medium", "high"] = "low"
    reproducible: bool = False

Form.from_model(
    BugReport,
    defaults={"title": "Login broken on Safari", "severity": "high", "reproducible": True},
    on_submit=CallTool("file_bug"),
)

Partial dicts are fine — missing keys fall back to the model's defaults. Unknown keys raise ValueError to catch typos. Date/time values accept either Python date/datetime/time instances or ISO strings, so FastMCP can forward raw JSON straight through. The date/time branches also now render value= at all (previously dropped it on the floor — even the class-level default never surfaced).

Also fixes a latent bug in tools/render_previews.py: the module's from __future__ import annotations was leaking through exec() and turning every preview's field annotation into a ForwardRef string, which silently broke Pydantic introspection — e.g. Literal[...] fields rendered as text inputs instead of selects. Compiling with dont_inherit=True isolates the preview.

@marvin-context-protocol marvin-context-protocol Bot added enhancement Improvement to existing functionality or new capabilities. python Related to the Python SDK: components, actions, serialization. labels Apr 14, 2026
@jlowin jlowin merged commit c228498 into main Apr 14, 2026
1 check passed
@jlowin jlowin deleted the form-from-model-defaults branch April 14, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improvement to existing functionality or new capabilities. python Related to the Python SDK: components, actions, serialization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant