Pydantic Integration#

In addition to standard dataclasses, tyro also supports Pydantic models.

 1from pydantic import BaseModel, Field
 2
 3import tyro
 4
 5
 6class Args(BaseModel):
 7    """Description.
 8    This should show up in the helptext!"""
 9
10    field1: str
11    field2: int = Field(3, description="An integer field.")
12
13
14if __name__ == "__main__":
15    args = tyro.cli(Args)
16    print(args)

python 04_additional/08_pydantic.py --help
usage: 08_pydantic.py [-h] --field1 STR [--field2 INT]

Description. This should show up in the helptext!

╭─ options ───────────────────────────────────────────╮
│ -h, --help          show this help message and exit │
│ --field1 STR        (required)                      │
│ --field2 INT        An integer field. (default: 3)  │
╰─────────────────────────────────────────────────────╯

python 04_additional/08_pydantic.py --field1 hello
field1='hello' field2=3

python 04_additional/08_pydantic.py --field1 hello --field2 5
field1='hello' field2=5