Skip to content

api

Class info

🛈 DocStrings

fill(): one call to get a value from the user, whatever the terminal can do.

fill async

fill(
    target: type[T] | FormTarget[T],
    *,
    title: str | None = None,
    initial: Mapping[str, Any] | None = None,
    ui: UI = "auto",
) -> T
fill(
    target: Mapping[str, Any],
    *,
    title: str | None = None,
    initial: Mapping[str, Any] | None = None,
    ui: UI = "auto",
) -> dict[str, Any]
fill(
    target: type[Any] | FormTarget[Any] | Mapping[str, Any],
    *,
    title: str | None = None,
    initial: Mapping[str, Any] | None = None,
    ui: UI = "auto",
) -> Any

Ask the user to fill a form and return the validated result.

Parameters:

Name Type Description Default
target type[Any] | FormTarget[Any] | Mapping[str, Any]

A JSON Schema (gives a dict), or a Python type such as a pydantic model or dataclass (gives an instance).

required
title str | None

Heading of the form (default: the schema's title).

None
initial Mapping[str, Any] | None

Starting values (default: the schema's defaults).

None
ui UI

textual (a form below the prompt), sequential (one prompt per field), or auto: textual when it's installed and the terminal is interactive.

'auto'

Raises:

Type Description
FormCancelledError

The user cancelled.

Source code in src/promptantic/api.py
async def fill(
    target: type[Any] | FormTarget[Any] | Mapping[str, Any],
    *,
    title: str | None = None,
    initial: Mapping[str, Any] | None = None,
    ui: UI = "auto",
) -> Any:
    """Ask the user to fill a form and return the validated result.

    Args:
        target: A JSON Schema (gives a dict), or a Python type such as a pydantic
            model or dataclass (gives an instance).
        title: Heading of the form (default: the schema's title).
        initial: Starting values (default: the schema's defaults).
        ui: ``textual`` (a form below the prompt), ``sequential`` (one prompt per field),
            or ``auto``: textual when it's installed and the terminal is interactive.

    Raises:
        FormCancelledError: The user cancelled.
    """
    form_target = target if isinstance(target, FormTarget) else target_for(target)
    if ui == "auto":
        ui = "textual" if _textual_usable() else "sequential"
    if ui == "textual":
        from promptantic.textual import fill_inline

        return await fill_inline(form_target, title=title, initial=initial)
    from promptantic.sequential import fill_sequential

    return await fill_sequential(form_target, title=title, initial=initial)