JSON and structured output
Request machine-readable responses and validate them before using them.
Request JSON output
For a model and route that support JSON mode, send response_format: {"type": "json_object"} and explicitly ask for JSON in the conversation. JSON mode requests a JSON object; it does not by itself enforce your application’s schema.
import json
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["BULKTOKEN_API_KEY"], base_url="https://bulktoken.ai/v1")
response = client.chat.completions.create(
model="glm-5.3",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "Return a JSON object with a summary string and a tags array of strings."},
{"role": "user", "content": "Summarize: The application now supports streaming API responses."},
],
)
choice = response.choices[0]
if choice.finish_reason != "stop" or not choice.message.content:
raise ValueError("No complete JSON answer was returned")
data = json.loads(choice.message.content)
if not isinstance(data.get("summary"), str) or not isinstance(data.get("tags"), list):
raise ValueError("Unexpected response shape")
if not all(isinstance(tag, str) for tag in data["tags"]):
raise ValueError("Every tag must be a string")
print(data)Schema enforcement is model-specific
Strict json_schema support and accepted schema keywords depend on the model and route. A structured-output capability in the catalog is not a guarantee that every OpenAI schema option is accepted. Verify the exact schema you need before relying on server-side enforcement.
Keep schema validation in your application. Handle refusals, truncated responses, invalid JSON, and missing fields explicitly; do not pass unchecked model output into downstream operations.