SDK Reference
Structured Output
SDK Reference
Structured Output
Consistent JSON Output
Maitai supports the use of JSON schemas or Pydantic models in order to get consistent JSON generation. This feature is available to all models that Maitai supports.
import maitai
from pydantic import BaseModel
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
maitai_client = maitai.MaitaiAsync()
completion = await maitai_client.chat.completions.create(
application="DEMO_APP",
intent="CALENDAR",
model="llama3.1-8b",
messages=[
{"role": "system", "content": "Extract the event information."},
{"role": "user", "content": "AI learning seminar, October 20."},
],
response_format=CalendarEvent,
)
event: CalendarEvent = completion.choices[0].message.parsed
JSON Example:
import maitai
schema = {
"name": "athlete_schema",
"strict": True,
"schema": {
"type": "object",
"properties": {
"athletes": {
"type": "array",
"items": {
"type": "object",
"properties": {
"sport": {"type": "string"},
"name": {"type": "string"},
},
"required": ["sport", "name"],
"additionalProperties": False,
},
}
},
"required": ["athletes"],
"additionalProperties": False,
},
}
maitai_client = maitai.MaitaiAsync()
response = await maitai_client.chat.completions.create(
session_id="YOUR_SESSION_ID",
intent="FACTS",
application="DEMO_APP",
model="claude-3-5-sonnet-20240620",
messages=[{"role": "user", "content": "Give me a list of 5 famous athletes and their sport"}],
response_format={"type": "json_schema", "json_schema": schema}
)
Was this page helpful?