Support for BaseModel
Author: JonasKsCreated Sep 30, 2026Updated Sep 30, 2026
Labelsenhancement
We utilize Monty in ways it might not be initially intended for, such as allowing agents to write persistent functions that will be executed by other agents later.
This works really well, but I have two wishes for Monty scripts:
- Input validation
- Output validation
Example of an persistent function (written by an LLM):
limit = input.get("limit", 20)
if not isinstance(limit, int) or limit < 1 or limit > 100:
raise ValueError("limit must be an integer between 1 and 100")
...
{"vendor_count": len(matches), "invoices": invoices}What I'd prefer this to be:
class Input(BaseModel):
limit: int = Field(default=20, ge=1, le=100)
class Invoice(BaseModel):
amount: float
class Output(BaseModel):
vendor_count: int
invoices: list[Invoice]
args = Input.model_validate(input)
...
Output(vendor_count=len(matches), invoices=invoices)If I had this, I would:
- Require every persistent function to declare
InputandOutputasBaseModelsubclasses, and refuse one that does not. - Wrap the body: validate
inputagainstInputbefore it runs, and the final expression againstOutputbefore it returns. A failure is aValidationErrornaming the field. - Derive the two JSON Schemas from the models and store them. Other agents see them as the function's
inputSchemaandoutputSchemawhen they list it as a tool.
Source: pydantic/monty