Skip to content

generator

Class info

Classes

Name Children Inherits
ModelGenerator
promptantic.generator
Fill a pydantic model interactively; prefer :func:`promptantic.fill`.

    🛈 DocStrings

    ModelGenerator: the classic API, now a thin wrapper around :func:promptantic.fill.

    ModelGenerator

    Fill a pydantic model interactively; prefer :func:promptantic.fill.

    The constructor's options (style, show_progress, ...) belonged to the old prompt-per-type implementation and are accepted but ignored.

    Source code in src/promptantic/generator.py
    class ModelGenerator:
        """Fill a pydantic model interactively; prefer :func:`promptantic.fill`.
    
        The constructor's options (``style``, ``show_progress``, ...) belonged to the old
        prompt-per-type implementation and are accepted but ignored.
        """
    
        def __init__(self, *_args: Any, **_kwargs: Any) -> None:
            pass
    
        @overload
        async def apopulate[M: BaseModel](self, model: type[M]) -> M: ...
        @overload
        async def apopulate[M: BaseModel](self, model: M) -> M: ...
        async def apopulate[M: BaseModel](self, model: type[M] | M) -> M:
            """An instance of ``model``; given an instance, its values are the starting values."""
            if isinstance(model, BaseModel):
                return await fill(type(model), initial=initial_values(model))
            return await fill(model)
    
        @overload
        def populate[M: BaseModel](self, model: type[M]) -> M: ...
        @overload
        def populate[M: BaseModel](self, model: M) -> M: ...
        def populate[M: BaseModel](self, model: type[M] | M) -> M:
            """Like :meth:`apopulate`, for code that isn't async."""
            return asyncio.run(self.apopulate(model))
    

    apopulate async

    apopulate(model: type[M]) -> M
    
    apopulate(model: M) -> M
    
    apopulate(model: type[M] | M) -> M
    

    An instance of model; given an instance, its values are the starting values.

    Source code in src/promptantic/generator.py
    async def apopulate[M: BaseModel](self, model: type[M] | M) -> M:
        """An instance of ``model``; given an instance, its values are the starting values."""
        if isinstance(model, BaseModel):
            return await fill(type(model), initial=initial_values(model))
        return await fill(model)
    

    populate

    populate(model: type[M]) -> M
    
    populate(model: M) -> M
    
    populate(model: type[M] | M) -> M
    

    Like :meth:apopulate, for code that isn't async.

    Source code in src/promptantic/generator.py
    def populate[M: BaseModel](self, model: type[M] | M) -> M:
        """Like :meth:`apopulate`, for code that isn't async."""
        return asyncio.run(self.apopulate(model))
    

    initial_values

    initial_values(model: BaseModel) -> dict[str, Any]
    

    The instance's values as form starting values, with secrets unmasked.

    model_dump(mode="json") masks SecretStr / SecretBytes as "**********"; accepting such a default would replace the real secret. The forms show secret fields masked anyway.

    Source code in src/promptantic/generator.py
    def initial_values(model: BaseModel) -> dict[str, Any]:
        """The instance's values as form starting values, with secrets unmasked.
    
        ``model_dump(mode="json")`` masks ``SecretStr`` / ``SecretBytes`` as
        ``"**********"``; accepting such a default would replace the real secret. The
        forms show secret fields masked anyway.
        """
        return to_jsonable_python(_unmask(model.model_dump()))