By default, a text generation model responds to your requests with a text in natural language. You can also force the model to respond with JSON instead, which makes it easier to work with outputs programmatically in your application.For example, you can tell a model the name of an actor or actress and get a response with details of a film they have starred in, in a format like this:
Use response_formatparameter to get JSON output. Depending on the settings in your request, the response can be:
JSON schema following. Follows the specific schema you provide - {"type": "json_schema"} in response_format parameter.
Arbitrary JSON object. Forces arbitrary JSON output, the model will decide on the schema by itself {"type": "json_object"} in response_format parameter.
Supported models
Some models are better in providing JSON due to their training data or our inference engine limitations. Use JSON mode tag in on a model card to find a model with structured output supported.
1
Choose response format type
Select between strict "json_schema" and arbitrary "json_object"
It’s always better to provide the instruction to follow JSON output and the schema to your model’s system or user prompt to increase output quality and consistency
4
Test several models
Different models has different structured output capabilities. Always test and compare a few to obtain the best result
JSON that follows a schema provided in your request. Make a request to the Nebius AI Studio API with {"type": "json_schema"} in response_format and provide desirable schema in a JSON Schema Specification compliant format.
Many practical tests show that it’s always better to provide the schema both in text prompt for the model and"json_schema"parameter
Copy
Ask AI
import osimport jsonfrom openai import OpenAIfrom typing import List, Literalfrom pydantic import BaseModel# 1. Define a schema using Pydantic's `BaseModel`. You can also define # a JSON Schema directly; see details after this code exampleclass Film(BaseModel): title: str year: int director: str cast: List[str] genre: Literal[ "drama", "thriller", "sci-fi", "comedy", "horror", "fantasy" ]# 2. Add the schema to your requestclient = OpenAI( base_url="https://api.studio.nebius.com/v1/", api_key=os.environ.get("NEBIUS_API_KEY"),)completion = client.chat.completions.create( model="Qwen/Qwen3-235B-A22B", response_format={ "type": "json_schema" "json_schema": Film.model_json_schema() } messages=[ { "role": "system", "content": ( "I will give you an actor or actress, and you will respond " "with details of a real film they have starred in, according " "to the provided structure." ) }, { "role": "user", "content": "Jack Nicholson" } ],)# 3. Work with the JSON output or a refusaloutput = completion.choices[0].messageif output.refusal: # Handle refusal print(output.refusal)elif output.content: try: output_json = json.loads(output.content) print(output_json) print("Film: {} ({})".format(output_json['title'], output_json['year'])) # etc. except Exception as e: # Handle possible exceptions, e.g. invalid JSON print(e) pass
The model will produce a valid JSON object without following any specific schema.
Make a request to the Nebius AI Studio API with {"type": "json_object"} in response_format:
Copy
Ask AI
import osimport jsonfrom openai import OpenAIclient = OpenAI( base_url="https://api.studio.nebius.com/v1/", api_key=os.environ.get("NEBIUS_API_KEY"),)completion = client.chat.completions.create( model="mistralai/Mistral-Nemo-Instruct-2407", # 1. Instruct the model to produce JSON. In this example, we do it # in the system prompt; you can do it in a user message instead messages=[ { "role": "system", "content": ( "I will give you an actor or actress, and you will respond " "with details of a real film they have starred in, using JSON " "as the format." ) }, { "role": "user", "content": "Jack Nicholson" } ], # 2. Set the response format to `json_object` response_format={ "type": "json_object" })# 3. Work with the JSON output (it should be valid but is not guaranteed # to have any predetermined fields) or a refusaloutput = completion.choices[0].messageif output.refusal: # Handle refusal print(output.refusal)elif output.content: try: output_json = json.loads(output.content) print(output_json) except Exception as e: # Handle possible exceptions, e.g. invalid JSON print(e) pass
Use JSON schema instead of JSON objects whenever you need strict JSON structure