- Guarantee structured output using our chat completions API
- This can be done using JSON Schema / Pydantic models, (schemaless) JSON Mode, regular expressions, and multiple choice.
Sentiment Analysis using Multiple Choice
One of the simplest forms of structured output is multiple choice. The only possible outputs of the following code snippet arepositive or negative.
Make sure you have set the
TELNYX_API_KEY environment variablezai-org/GLM-5.2 is a reasoning model. Your structured output still arrives in
choices[0].message.content — the model’s chain-of-thought is returned separately in
choices[0].message.reasoning_content, so it never pollutes the JSON you parse. To
surface the reasoning, read it alongside content:Building on this with guided_json
Now let’s say we want to capture an explanation for the classification as well. We can leverage the guided_json field for this.