Force the model to emit valid JSON
Set response_mime_type='application/json' and the model will produce JSON. Add a response_schema and the JSON will conform to that schema. The schema can be a Pydantic model, a TypedDict, or a raw dict.
Two cleanups it does for you
- No markdown fences (
```json...```) wrapping the output. - No trailing prose ("Here's the JSON: ...").
You get a clean parsable string in response.text, and (if you used Pydantic) a deserialized object in response.parsed.
This is not the same as function calling
- Structured output = format the final response. The model talks back to you in JSON.
- Function calling = the model wants to call your code mid-conversation. The function call is the response, not the final answer.
Use structured output for: extraction, classification, form-filling. Use function calling for: anything that requires action in your system.