Skip to main content
Structured output (JSON mode) ensures that AI returns JSON data conforming to predefined formats, providing reliable guarantees for programmatic processing of AI outputs.

Features

The structured output feature provides AI models with strict data format control capabilities, supporting various complex data structures and validation requirements.

Core Parameters

  • response_format: Specifies the response format, set to {"type": "json_object"} to enable JSON mode
  • model: Use models that support structured output, such as glm-5, glm-4.7, glm-4.5, glm-4.6, etc.
  • messages: Define the expected JSON structure and field requirements in system messages

Code Examples

Install SDK
Verify Installation
Complete Example The following is a complete structured output example demonstrating how to perform sentiment analysis and return structured JSON results:

Basic Usage

Simple JSON Output

Scenario Examples

When using JSON mode for data extraction, please ensure the quality and format of input data to achieve the best extraction results.

Best Practices

Schema Design Principles

  • Clarity: Field names and types should be clear and explicit
  • Completeness: Include all necessary validation rules
  • Flexibility: Consider future expansion needs

Error Handling Strategy

  • Multi-layer validation: Schema validation + business logic validation
  • Fallback plan: Prepare simplified backup Schema
  • Logging: Record detailed error information
JSON mode requires AI to strictly output according to specified format, but in some complex scenarios it may affect the naturalness of responses. It’s recommended to find a balance between functionality and user experience.
When designing JSON Schema, it’s recommended to start with simple structures and gradually increase complexity. Also, providing detailed descriptions and examples for key fields helps AI better understand and generate JSON data that meets requirements.