註冊並分享邀請連結,可獲得影片播放與邀請獎勵。

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture
加入 May 2026
240 正在關注    207 粉絲
# Practical and Useful Patterns for OpenAI Agent SDK 🌍 Stop praying that your LLM returns valid JSON. With output_type, you get type-safe structured data every time -- no more broken JSON headaches. Simply specify a Pydantic model, dataclass, or TypedDict as output_type, and your agent's output is automatically validated structured data. 📌 Title: Agents -- Output types 🔗 URL: 🧩 Overview Setting the `output_type` parameter on an Agent enforces Structured Outputs from the LLM. You can specify a Pydantic BaseModel, dataclass, or TypedDict, and the output is automatically parsed and validated. This lets downstream code safely work with structured data. When output_type is set, the agent's final output becomes an object of the specified type rather than plain text. 🛠 How to Use Define a `CalendarEvent` class extending `BaseModel` with fields `name: str`, `date: str`, and `participants: list[str]`, then set `output_type=CalendarEvent` on the `Agent`. When you call ` ...)`, ` is returned as a `CalendarEvent` instance, allowing type-safe access to ` and `event.participants`. 🏗 Practical Usage Patterns **Extract Calendar Events from Emails and Register via API** A pipeline that passes structured output directly to an external API. Run ` email_body)` to get ` as a `CalendarEvent`, then pass it directly to `calendar_api.create_event(title= date= attendees=event.participants)` without any manual parsing. **Enum Output for Branch Orchestration** Return classification as an enum and use code to deterministically branch. Define `TicketCategory(str, Enum)` with values `billing`, `technical`, and `general`, then create a `Classification(BaseModel)` with `category: TicketCategory` and `confidence: float`. Set `output_type=Classification` on the `Agent`, run it, and use a `match` statement on ` to deterministically route to `handoff_to_billing`, `handoff_to_engineering`, or `handoff_to_general`. **Type Guarantees for Business-Critical Processing** For workflows where broken JSON is absolutely unacceptable, Structured Outputs serve as a safety net. Define `InvoiceData(BaseModel)` with fields `invoice_number: str`, `amount: float`, `currency: str`, `due_date: str`, and `line_items: list[dict[str, str | float]]`, then set `output_type=InvoiceData` on the `Agent` to guarantee type-safe structured extraction of invoice data. 💡 Use Cases 📧 Extract CalendarEvent(name, date, participants) from emails and auto-register via calendar API 🏷 Classify support tickets with Enum output and deterministically route by category in code 💰 Type-safe structured extraction from invoices and contracts where broken JSON is unacceptable 📊 Convert free-text survey responses into structured data and feed directly into analytics pipelines ⚠️ Caveats - When output_type is set, the agent's final output is always that type. It cannot return plain text responses, so omit output_type if you need text replies. - Overly complex nested structures can reduce LLM output accuracy. Keep structures as flat as possible. - Use Optional fields appropriately to allow None when the LLM cannot find certain information. ✨ With output_type, you can plug LLM outputs directly into business logic. Move from "parse and pray" to "guarantee with types" -- a significant step forward in agent development! #OpenAIAgentSDK# #AIAgent#
顯示更多