frompydanticimportBaseModelfromopenaiimportOpenAIclient=OpenAI()classCalendarEvent(BaseModel):name:strdate:strparticipants:list[str]completion=client.beta.chat.completions.parse(model="MODEL_NAME",messages=[{"role":"system","content":"Extract the event information."},{"role":"user","content":"Alice and Bob are going to a science fair on Friday."},],response_format=CalendarEvent,)print(completion.choices[0].message.parsed)
frompydanticimportBaseModelfromopenaiimportOpenAIfromenumimportEnumfromtypingimportListclient=OpenAI()classUIType(str,Enum):div="div"button="button"header="header"section="section"field="field"form="form"classAttribute(BaseModel):name:strvalue:strclassUI(BaseModel):type:UITypelabel:strchildren:List["UI"]attributes:List[Attribute]UI.model_rebuild()# This is required to enable recursive typesclassResponse(BaseModel):ui:UIcompletion=client.beta.chat.completions.parse(model="MODEL_NAME",messages=[{"role":"system","content":"You are a UI generator AI. Convert the user input into a UI."},{"role":"user","content":"Make a User Profile Form"}],response_format=Response,)print(completion.choices[0].message.parsed)
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-25 (世界標準時間)。"],[],[],null,["Structured outputs enable a model to generate output that always adheres to a\nspecific schema. For example, a model may be provided with a response schema to\nensure that the response produces valid JSON. All open models available on the\nVertex AI Model as a Service (MaaS) support structured outputs.\n\nFor more conceptual information about the structured output capability, see\n[Introduction to structured output](../../multimodal/control-generated-output).\n\nUse structured outputs\n\nThe following use case sets a response schema that ensures that the model output\nis a JSON object with the following properties: name, date, and participants.\nThe Python code uses the OpenAI SDK and Pydantic objects to generate the JSON\nschema. \n\n from pydantic import BaseModel\n from openai import OpenAI\n\n client = OpenAI()\n\n class CalendarEvent(BaseModel):\n name: str\n date: str\n participants: list[str]\n\n completion = client.beta.chat.completions.parse(\n model=\"\u003cvar label=\"model name\" translate=\"no\"\u003eMODEL_NAME\u003c/var\u003e\",\n messages=[\n {\"role\": \"system\", \"content\": \"Extract the event information.\"},\n {\"role\": \"user\", \"content\": \"Alice and Bob are going to a science fair on Friday.\"},\n ],\n response_format=CalendarEvent,\n )\n\n print(completion.choices[0].message.parsed)\n\n| **Note:** The response has a `CalendarEvent` object, which is populated with the defined fields. The model outputs JSON, which the SDK parses into a Pydantic object.\n\nThe model output will adhere to the following JSON schema: \n\n { \"name\": STRING, \"date\": STRING, \"participants\": [STRING] }\n\nWhen provided the prompt, \"Alice and Bob are going to a science fair on Friday\",\nthe model could produce the following response: \n\n {\n \"name\": \"science fair\",\n \"date\": \"Friday\",\n \"participants\": [\n \"Alice\",\n \"Bob\"\n ]\n }\n\nDetailed example\n\nThe following code is an example of a recursive schema. The `UI` class contains\na list of `children`, which can also be of the `UI` class. \n\n from pydantic import BaseModel\n from openai import OpenAI\n from enum import Enum\n from typing import List\n\n client = OpenAI()\n\n class UIType(str, Enum):\n div = \"div\"\n button = \"button\"\n header = \"header\"\n section = \"section\"\n field = \"field\"\n form = \"form\"\n\n class Attribute(BaseModel):\n name: str\n value: str\n\n class UI(BaseModel):\n type: UIType\n label: str\n children: List[\"UI\"]\n attributes: List[Attribute]\n\n UI.model_rebuild() # This is required to enable recursive types\n\n class Response(BaseModel):\n ui: UI\n\n completion = client.beta.chat.completions.parse(\n model=\"\u003cvar label=\"model name\" translate=\"no\"\u003eMODEL_NAME\u003c/var\u003e\",\n messages=[\n {\"role\": \"system\", \"content\": \"You are a UI generator AI. Convert the user input into a UI.\"},\n {\"role\": \"user\", \"content\": \"Make a User Profile Form\"}\n ],\n response_format=Response,\n )\n\n print(completion.choices[0].message.parsed)\n\nThe model output will adhere to the schema of the Pydantic object specified in\nthe previous snippet. In this example, the model could generate the following UI\nform: \n\n Form\n Input\n Name\n Email\n Age\n\nA response could look like the following: \n\n ui = UI(\n type=UIType.div,\n label='Form',\n children=[\n UI(\n type=UIType.div,\n label='Input',\n children=[],\n attributes=[\n Attribute(name='label', value='Name')\n ]\n ),\n UI(\n type=UIType.div,\n label='Input',\n children=[],\n attributes=[\n Attribute(name='label', value='Email')\n ]\n ),\n UI(\n type=UIType.div,\n label='Input',\n children=[],\n attributes=[\n Attribute(name='label', value='Age')\n ]\n )\n ],\n attributes=[\n Attribute(name='name', value='John Doe'),\n Attribute(name='email', value='john.doe@example.com'),\n Attribute(name='age', value='30')\n ]\n )"]]