Skip to main content
In this tutorial, you’ll learn how to:
  • Guarantee structured output using our chat completions API
  • This can be done using JSON Schema / Pydantic models, (schemaless) JSON Mode, and multiple choice.

Sentiment Analysis using Multiple Choice

One of the simplest forms of structured output is multiple choice. The schema below pins the classification to a single value — the response is always {"sentiment": "positive"} or {"sentiment": "negative"}.
Make sure you have set the TELNYX_API_KEY environment variable
zai-org/GLM-5.3-Flash 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:
Since the schema’s sentiment property is an enum with exactly two values, the model can only answer with one of positive or negative:

Constraining the full response shape with json_schema

Now let’s say we want to capture an explanation for the classification as well. The response_format parameter with "type": "json_schema" guarantees the response parses as JSON matching your schema, and "additionalProperties": false on an object schema makes the response carry exactly the declared properties — without it, JSON Schema permits extra keys, so a schema listing sentiment and explanation doesn’t by itself forbid a third key:
This will ensure a JSON response with the same schema as the following
The guided_json, guided_choice, and guided_regex request fields are accepted for compatibility with the vLLM/SGLang serving stack, but they are not enforced for Telnyx-hosted models today: a request carrying them is served as a plain chat completion, so the model may return free text that ignores the schema or choice list. Use response_format with "type": "json_schema" for guaranteed structured output on Telnyx-hosted models.

Simplifying Schema Generation using Pydantic

The above is helpful to see the raw JSON Schema specification being sent via API. However, for practical purposes, using Pydantic models can help simplify generating the specs. The following is functionally equivalent to the previous example.

Schema-less JSON Mode

We also support the schema-less JSON mode provided by OpenAI