SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
919 B · 25 lines python
Raw Blame History
1#!/usr/bin/env python32# STATUS: LIVE_VERIFIED — executed 2026-09-18 (exit 0 in 1.3s)3# Structured Outputs with the SDK's parse() helper (Pydantic model -> strict JSON schema in text.format).4import pathlib5import sys67ROOT = pathlib.Path(__file__).resolve().parents[3]8sys.path.insert(0, str(ROOT))9from scripts import live  # noqa: E4021011from openai import OpenAI  # noqa: E40212from pydantic import BaseModel  # noqa: E402131415class OkReply(BaseModel):16    answer: str171819client = OpenAI()20response = client.responses.parse(model="gpt-5.4-nano", input="Reply with OK.", max_output_tokens=32, text_format=OkReply)21live.log_request("openai", "POST", "/v1/responses", 200, 0.00003, "example responses/structured_output.py")22print(response.output_parsed)          # OkReply(answer='OK')23print(response.text.format.model_dump())  # echoed json_schema format (strict=True)24assert isinstance(response.output_parsed, OkReply)25