# Structured Output

### Overview

Parasail APIs offer built-in support for structured outputs, enabling easy integration and reliable data parsing.

### Best Practices

* Clearly define your schema in the prompt.
* Use simple and direct parameters to facilitate predictable responses.
* Validate responses against your expected schema.

By using structured outputs, Parasail helps developers maintain consistency and integration simplicity across applications.

## Currently Supported Models:

| Models                                 | Structured Outputs | JSON Schema | Python Object | Regex |
| -------------------------------------- | ------------------ | ----------- | ------------- | ----- |
| parasail-llama-33-70b-fp8              | Y                  | Y           |               | Y     |
| parasail-llama-4-scout-instruct        | Y                  | Y           | Y             | Y     |
| parasail-llama-4-maverick-instruct-fp8 | Y                  | Y           | Y             | Y     |
| parasail-qwen3-30b-a3b                 | Y                  | Y           | Y             |       |
| parasail-qwen3-235b-a22b               | Y                  | Y           | Y             |       |
| parasail-qwen3-32b                     | Y                  | Y           | Y             |       |
| parasail-gemma3-27b-it                 | Y                  | Y           | Y             | Y     |
| parasail-mistral-devstral-small        | Y                  | Y           | Y             | Y     |

### Structured Output Formats

Parasail supports several structured output formats based on OpenAI specifications:

#### 1. JSON Schema

Define structured responses using JSON schema to ensure clear data contracts.

**Prompt Example:**

```
{
  "prompt": "What's the weather like in Manhattan Beach on 2025-06-03?",
  "structured_output": {
    "type": "object",
    "properties": {
      "location": {"type": "string"},
      "date": {"type": "string", "format": "date"}
    },
    "required": ["location", "date"]
  }
}
```

**Expected Response:**

```
{
  "location": "Manhattan Beach",
  "date": "2025-06-03"
}
```

#### 2. Python Object

Responses structured directly as Python dictionaries for seamless Python integrations.

**Prompt Example:**

```
{
  "prompt": "Return the weather info for Manhattan Beach on 2025-06-03.",
  "structured_output": "dict"
}
```

**Expected Response (Model Dependent):**

```
{
  "location": "Manhattan Beach",
  "date": "2025-06-03"
}
```

#### 3. Regex

Define structured outputs using regular expressions to match specific response patterns.

**Prompt Example:**

```
{
  "prompt": "Extract the date from: 'Weather forecast for Manhattan Beach on 2025-06-03'.",
  "structured_output": "\\d{4}-\\d{2}-\\d{2}"
}
```

**Expected Response:**

```
"2025-06-03"
```

### Making an API Call

Send structured output requests using Parasail's inference endpoint:

```python
from openai import OpenAI

# Configure the OpenAI client to point at the Parasail-compatible endpoint
client = OpenAI(
    base_url="https://api.parasail.ai/v1",  # Parasail OpenAI‑compatible server
    api_key="YOUR_API_KEY",
)

# Build and send the request with an OpenAI‑style guided_json parameter
completion = client.chat.completions.create(
    model="parasail-qwen3-32b",
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Manhattan Beach on 2025-06-03?",
        }
    ],
    extra_body={
        "guided_json": {
            "type": "object",
            "properties": {
                "location": {"type": "string"},
                "date": {"type": "string", "format": "date"},
            },
            "required": ["location", "date"],
        }
    },
)

# The response is already validated by guided decoding to match the schema
print(completion.choices[0].message.content)
```

***

### Examples:

Using the Parasail Serverless Chat Example:

```
curl https://api.parasail.io/v1/chat/completions \
 -H "Authorization: Bearer $PARASAIL_API_KEY" \
 -H "Content-Type: application/json" \
 -d '{
       "model": "parasail-qwen3-32b",
       "messages": [{"role":"user","content":"Return today’s date only."}],
       "structured_output":"\\d{4}-\\d{2}-\\d{2}"
     }'
```

**JSON Example:**

**Using**: parasail-qwen3-32b (to enable thinking, remove /no\_think in the prompt to remove thinking)

**Input:**

```
/no_think
{
"prompt": "What's the weather like in Manhattan Beach on 2025-06-03?",
"structured_output": {
"type": "object",
"properties": {
"location": {"type": "string"},
"date": {"type": "string", "format": "date"}
},
"required": ["location", "date"]
}
}
```

**Output**

````
```json
{
"location": "Manhattan Beach",
"date": "2025-06-03"
}
```
````

**Python Object:**

**Using**: parasail-qwen3-32b (to enable thinking, remove /no\_think in the prompt to remove thinking)

**Input:**

```
/no_think
{
"prompt": "Return the weather info for Manhattan Beach on 2025-06-03.",
"structured_output": "dict"
}
```

**Output:**

````
```json
{
"location": "Manhattan Beach, CA",
"date": "2025-06-03",
"weather": {
"condition": "Partly Cloudy",
"high_temperature": "72°F",
"low_temperature": "60°F",
"wind": {
"speed": "10 mph",
"direction": "WNW"
},
"precipitation": "0%",
"humidity": "68%",
"uv_index": 5
}
}
```
````

**Regex:**

JSON Example:

**Using**: parasail-qwen3-32b (to enable thinking, remove /no\_think in the prompt to remove thinking)

**Input:**

```
/no_think

{
"prompt": "Extract the date from: 'Weather forecast for Manhattan Beach on 2025-06-03'.",
"structured_output": "\\d{4}-\\d{2}-\\d{2}"
}
```

**Output:**

<pre><code><strong>The extracted date is: **2025-06-03**.
</strong></code></pre>

***

#### Parasail Structured Output—**5-Minute Quick-Start**

> *Perfect for someone who just wants to see it work once before reading the details.*

***

1. **Prerequisites**

   ```bash
   bashCopyEditpip install openai   # Parasail is OpenAI-compatible
   export PARASAIL_API_KEY="sk-..."      # paste your key
   ```
2. **One-file demo**

   Save this as `structured_demo.py`:

   ```python
   pythonCopyEditimport openai, json

   openai.api_key  = "sk-..."            # or rely on the env-var
   openai.api_base = "https://api.parasail.io/v1"

   # Ask a question and force the model to answer in JSON
   schema = {
       "type": "object",
       "properties": {
           "capital": {"type": "string"},
           "country": {"type": "string"}
       },
       "required": ["capital", "country"],
       "additionalProperties": False
   }

   resp = openai.ChatCompletion.create(
       model="parasail-qwen3-32b",             # any listed compatible model
       messages=[{"role": "user",
                  "content": "What is the capital of France?"}],
       structured_output=schema               # <—— the only new field
   )

   data = resp.choices[0].message.content     # already valid JSON
   print(json.dumps(json.loads(data), indent=2))
   ```
3. **Run it**

   ```bash
   bashCopyEditpython structured_demo.py
   ```

   Expected output:

   ```json
   jsonCopyEdit{
     "capital": "Paris",
     "country": "France"
   }
   ```
4. **Swap constraints in one line**

   *Want a date instead?*\
   Replace the `schema` with a regex:

   ```python
   pythonCopyEditstructured_output = r"\d{4}-\d{2}-\d{2}"   # ISO date
   ```

   *Want a fixed choice?*

   ```python
   pythonCopyEditstructured_output = ["red", "green", "blue"]
   ```

***

### Troubleshooting tips:

| Symptom                               | Likely Cause                   | Fix                                                                   |
| ------------------------------------- | ------------------------------ | --------------------------------------------------------------------- |
| Model ignores constraint              | Prompt overshadowed parameter  | Put the schema/regex in **both** your prompt and `structured_output`. |
| Response is valid JSON but wrong keys | `additionalProperties` not set | Add `"additionalProperties": false` to your schema.                   |
| Streaming needed                      | Large JSON output              | Use `stream=True`—constraints still apply.                            |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.parasail.io/parasail-docs/cookbooks/structured-output.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
