Structured Data with ASI:One
ASI:One can return responses that conform to a JSON schema you define. This example shows how to extract structured information—a CalendarEvent
—directly from natural language using tool calling and Pydantic for type-safe parsing.
from pydantic import BaseModel
from enum import Enum
import uuid
import json
from openai import OpenAI, pydantic_function_tool
# ASI1 API config
ASI_API_KEY = "<YOUR_ASI_API_KEY>"
ASI_BASE_URL = "https://api.asi1.ai/v1"
# Calendar event model
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
# Init client
client = OpenAI(api_key=ASI_API_KEY, base_url=ASI_BASE_URL)
session_id = str(uuid.uuid4())
print("🔄 Sending calendar-event request to ASI1 experimental")
print(f"🆔 Session ID: {session_id}\n")
completion = client.beta.chat.completions.parse(
model="asi1-experimental",
messages=[
{"role": "system", "content": "Extract the event information by calling the calendar_event function."},
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."}
],
tools=[pydantic_function_tool(CalendarEvent, name="calendar_event")],
extra_headers={"x-session-id": session_id}
)
if completion.choices[0].message.tool_calls:
event: CalendarEvent = completion.choices[0].message.tool_calls[0].function.parsed_arguments
print("✅ CalendarEvent parsed: \n")
print(json.dumps(event.model_dump(), indent=2))
else:
print("⚠️ No tool call returned. Raw message:")
print(completion.choices[0].message)
print(f"\n🆔 Session ID for debugging: {session_id}")
Expected Output
The assistant calls the calendar_event
tool and the parsed result is automatically converted into a CalendarEvent
instance:
{
"name": "science fair",
"date": "Friday",
"participants": [
"Alice",
"Bob"
]
}
You now have strongly-typed data ready for further processing (saving to a database, triggering notifications, etc.).
Next Steps
- Learn how to define tool schemas
- See the Chat Completion guide for more request/response patterns