Skip to Content
WikiInterface FeaturesStructured Output

JSON Mode - MyTokenGate

1. Use Cases

Currently, the MyTokenGate large model API platform, MyTokenGate, by default generates unstructured text. However, in certain application scenarios, you may want the model to output content in a structured format, but simply instructing the model via prompts might not yield the desired structured output.

As a standardized and lightweight data exchange format, JSON mode is an important feature for enabling structured outputs from large model APIs. When you make a request to the large model API, the results returned by the model are presented in JSON format, which is easy for humans to read and write, and also easy for machines to parse and generate.

Now, on the MyTokenGate platform, apart from vision/image-generation models, most major language models support JSON mode. This allows the model to output strings in JSON format, ensuring that the model produces outputs in the expected structure, facilitating subsequent logical parsing of the output content.

For example, you can now use the MyTokenGate API to attempt structured outputs for the following cases:

  • Build a news database from company-related reports, including news titles, links, etc.
  • Extract sentiment analysis structures from product purchase reviews, including sentiment polarity (positive, negative, neutral), sentiment intensity, sentiment keywords, etc.
  • Extract product lists from purchase histories, including product information, reasons for recommendation, prices, promotional details, etc.

2. How to Use

Add the following to your request:

response_format={"type": "json_object"}

3. Supported Models

The mainstream large language models offered on the platform support the parameter above.

4. Example Usage

Below is an example using OpenAI:

import json from openai import OpenAI client = OpenAI( api_key="Your APIKEY", # Obtain from https://mytokengate.com/app/dashboard base_url="https://gateway.mytokengate.com/v1" ) response = client.chat.completions.create( model="gpt-5.4", messages=[ {"role": "system", "content": "You are a helpful assistant designed to output JSON."}, {"role": "user", "content": "Who were the men's and women's singles champions in table tennis at the 2020 World Olympics? " "Please respond in the format {\"Men's Champion\": ..., \"Women's Champion\": ...}"} ], response_format={"type": "json_object"} ) print(response.choices[0].message.content)

The model will output:

{"Men's Champion": "Ma Long", "Women's Champion": "Chen Meng"}

5. Strict Mode (JSON Schema)

Beyond json_object, models that support structured output also accept the stricter json_schema form. You can declare fields, types, and required keys so the model conforms exactly to the given schema, removing the need for downstream validation:

response_format={ "type": "json_schema", "json_schema": { "name": "sentiment", "strict": True, "schema": { "type": "object", "properties": { "polarity": {"type": "string", "enum": ["positive", "negative", "neutral"]}, "score": {"type": "number"} }, "required": ["polarity", "score"], "additionalProperties": False } } }

response_format is passed through to the upstream unchanged; whether the schema is strictly enforced depends on the selected model’s support. Models without strict mode can fall back to json_object.

Last updated on