> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inceptionlabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Structured Outputs

> Constrain Mercury 2 chat completions to a JSON schema using structured outputs, so your application receives strictly typed, machine-parseable responses.

Use JSON schemas to enforce structured data output with specific properties, types, and constraints.

Export your API key as an environment variable in your terminal.

<CodeGroup>
  ```bash macOS / Linux theme={null}
  export INCEPTION_API_KEY="your_api_key_here"
  ```

  ```bash Windows theme={null}
  set INCEPTION_API_KEY="your_api_key_here"
  ```
</CodeGroup>

<CodeGroup>
  ```python Python theme={null}
  from inceptionai import Inception

  client = Inception()  # reads INCEPTION_API_KEY from the environment

  response_schema = {
      "name": "Sentiment",
      "strict": True,
      "schema": {
          "type": "object",
          "properties": {
              "sentiment": {
                  "type": "string",
                  "enum": ["positive", "negative", "neutral"]
              },
              "confidence": {
                  "type": "number",
                  "minimum": 0,
                  "maximum": 1
              },
              "key_phrases": {
                  "type": "array",
                  "items": {"type": "string"}
              }
          },
          "required": ["sentiment", "confidence", "key_phrases"]
      },
  }

  completion = client.chat.completions.create(
      model="mercury-2",
      messages=[
          {
              "role": "user",
              "content": "Analyze the sentiment of this text: 'I absolutely love this feature! It works perfectly and saves me so much time.'"
          }
      ],
      response_format={"type": "json_schema", "json_schema": response_schema},
  )
  print(completion.choices[0].message.content)
  ```

  ```typescript TypeScript theme={null}
  import Inception from 'inceptionai';

  const client = new Inception(); // reads INCEPTION_API_KEY from the environment

  const responseSchema = {
    name: 'Sentiment',
    strict: true,
    schema: {
      type: 'object',
      properties: {
        sentiment: { type: 'string', enum: ['positive', 'negative', 'neutral'] },
        confidence: { type: 'number', minimum: 0, maximum: 1 },
        key_phrases: { type: 'array', items: { type: 'string' } },
      },
      required: ['sentiment', 'confidence', 'key_phrases'],
    },
  };

  const completion = await client.chat.completions.create({
    model: 'mercury-2',
    messages: [
      {
        role: 'user',
        content:
          "Analyze the sentiment of this text: 'I absolutely love this feature! It works perfectly and saves me so much time.'",
      },
    ],
    response_format: { type: 'json_schema', json_schema: responseSchema },
  });
  console.log(completion.choices[0]?.message.content);
  ```
</CodeGroup>
