import datetime
from pydantic import BaseModel, Field
from vlmrun.client import VLMRun
client = VLMRun(
base_url="https://agent.vlm.run/v1", api_key="<VLMRUN_API_KEY>",
)
class Invoice(BaseModel):
invoice_number: str = Field(description="The number of the invoice")
invoice_date: datetime.date = Field(description="The date of the invoice")
total_amount: float = Field(description="The total amount of the invoice")
vendor_name: str = Field(description="The name of the vendor")
# Ask the agent for structured output using a strict JSON Schema
response = client.agent.completions.create(
... # Same as above
response_format={"type": "json_schema", "json_schema": Invoice.model_json_schema()},
)
# Validate and print the response
invoice = Invoice.model_validate_json(response.choices[0].message.content)
print(invoice)
>>> Invoice(invoice_number="INV-2024-001", date="2024-09-15", total_amount=1250.00, vendor_name="Acme Corporation")