Skip to content

Commit 4e01e67

Browse files
committed
Add structured output support (JSON mode and JSON Schema)
1 parent a54d6f1 commit 4e01e67

File tree

7 files changed

+793
-3
lines changed

7 files changed

+793
-3
lines changed

docs/guides/server.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,65 @@ GET /health
101101

102102
Returns server status.
103103

104+
## Structured Output (JSON Mode)
105+
106+
Force the model to return valid JSON using `response_format`:
107+
108+
### JSON Object Mode
109+
110+
Returns any valid JSON:
111+
112+
```python
113+
response = client.chat.completions.create(
114+
model="default",
115+
messages=[{"role": "user", "content": "List 3 colors"}],
116+
response_format={"type": "json_object"}
117+
)
118+
# Output: {"colors": ["red", "blue", "green"]}
119+
```
120+
121+
### JSON Schema Mode
122+
123+
Returns JSON matching a specific schema:
124+
125+
```python
126+
response = client.chat.completions.create(
127+
model="default",
128+
messages=[{"role": "user", "content": "List 3 colors"}],
129+
response_format={
130+
"type": "json_schema",
131+
"json_schema": {
132+
"name": "colors",
133+
"schema": {
134+
"type": "object",
135+
"properties": {
136+
"colors": {
137+
"type": "array",
138+
"items": {"type": "string"}
139+
}
140+
},
141+
"required": ["colors"]
142+
}
143+
}
144+
}
145+
)
146+
# Output validated against schema
147+
data = json.loads(response.choices[0].message.content)
148+
assert "colors" in data
149+
```
150+
151+
### Curl Example
152+
153+
```bash
154+
curl http://localhost:8000/v1/chat/completions \
155+
-H "Content-Type: application/json" \
156+
-d '{
157+
"model": "default",
158+
"messages": [{"role": "user", "content": "List 3 colors"}],
159+
"response_format": {"type": "json_object"}
160+
}'
161+
```
162+
104163
## Curl Examples
105164

106165
### Chat

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ dependencies = [
5050
"uvicorn>=0.23.0",
5151
# MCP (Model Context Protocol) support
5252
"mcp>=1.0.0",
53+
# JSON Schema validation for structured output
54+
"jsonschema>=4.0.0",
5355
]
5456

5557
[project.optional-dependencies]

0 commit comments

Comments
 (0)