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

# Chat Completions

> Guard OpenAI Chat Completions calls, tools, continuations, and streamed text.

Use this page when your agent still uses OpenAI Chat Completions. The OpenAI wrappers guard supported chat calls without changing the native client shape.

## Guarded Methods

| Method                                                  | JavaScript | Python                     |
| ------------------------------------------------------- | ---------- | -------------------------- |
| `client.chat.completions.create(...)`                   | Supported  | Supported                  |
| `client.chat.completions.create({ stream: true, ... })` | Supported  | Supported as `stream=True` |

## Basic Request

<CodeGroup>
  ```typescript TypeScript theme={null}
  const completion = await client.chat.completions.create({
    model: process.env.OPENAI_MODEL ?? "gpt-5.4-mini",
    messages: [
      {
        role: "user",
        content: "Summarize password reset link expiration guidance.",
      },
    ],
  });

  console.log(completion.choices[0]?.message.content);
  ```

  ```python Python theme={null}
  completion = client.chat.completions.create(
      model=os.environ.get("OPENAI_MODEL", "gpt-5.4-mini"),
      messages=[
          {
              "role": "user",
              "content": "Summarize password reset link expiration guidance.",
          }
      ],
  )

  print(completion.choices[0].message.content)
  ```
</CodeGroup>

Averta checks the message payload before OpenAI sees it. If the response contains final text, Averta checks that output before returning it.

## Tools

<CodeGroup>
  ```typescript TypeScript theme={null}
  const completion = await client.chat.completions.create({
    model: process.env.OPENAI_MODEL ?? "gpt-5.4-mini",
    messages,
    tools: [
      {
        type: "function",
        function: {
          name: "search_docs",
          description: "Search internal support documentation.",
          strict: true,
          parameters: {
            type: "object",
            properties: {
              query: { type: "string" },
            },
            required: ["query"],
            additionalProperties: false,
          },
        },
      },
    ],
  });
  ```

  ```python Python theme={null}
  completion = client.chat.completions.create(
      model=os.environ.get("OPENAI_MODEL", "gpt-5.4-mini"),
      messages=messages,
      tools=[
          {
              "type": "function",
              "function": {
                  "name": "search_docs",
                  "description": "Search internal support documentation.",
                  "strict": True,
                  "parameters": {
                      "type": "object",
                      "properties": {"query": {"type": "string"}},
                      "required": ["query"],
                      "additionalProperties": False,
                  },
              },
          }
      ],
  )
  ```
</CodeGroup>

For Chat Completions tools, Averta can:

* normalize function and custom tools
* remove blocked tools before forwarding the request
* sanitize `tool_choice` when the requested tool was blocked
* check returned tool calls before your app executes them

## Tool Result Continuation

Send tool results back as `tool` role messages through the wrapped client.

<CodeGroup>
  ```typescript TypeScript theme={null}
  const nextCompletion = await client.chat.completions.create({
    model: process.env.OPENAI_MODEL ?? "gpt-5.4-mini",
    messages: [
      ...messages,
      completion.choices[0]!.message,
      {
        role: "tool",
        tool_call_id: "call_123",
        content: JSON.stringify({
          matches: ["Password reset links expire after 24 hours."],
        }),
      },
    ],
    tools,
  });
  ```

  ```python Python theme={null}
  assistant_message = completion.choices[0].message.model_dump(exclude_none=True)

  next_completion = client.chat.completions.create(
      model=os.environ.get("OPENAI_MODEL", "gpt-5.4-mini"),
      messages=[
          *messages,
          assistant_message,
          {
              "role": "tool",
              "tool_call_id": "call_123",
              "content": '{"matches":["Password reset links expire after 24 hours."]}',
          },
      ],
      tools=tools,
  )
  ```
</CodeGroup>

Averta evaluates pure tool-result continuation requests before OpenAI sees them.

## Streaming

Chat Completions streaming is guarded in both OpenAI wrappers:

<CodeGroup>
  ```typescript TypeScript theme={null}
  const stream = await client.chat.completions.create({
    model: process.env.OPENAI_MODEL ?? "gpt-5.4-mini",
    messages,
    stream: true,
  });
  ```

  ```python Python theme={null}
  stream = client.chat.completions.create(
      model=os.environ.get("OPENAI_MODEL", "gpt-5.4-mini"),
      messages=messages,
      stream=True,
  )
  ```
</CodeGroup>

It currently supports one streamed text choice. Requests with streaming enabled and `n > 1` fail closed before provider execution.

## Current Limits

* Chat Completions streaming supports one streamed text choice.
* Streaming output rewrite is not supported yet.
* Rich media request preflight supports Data URL images in `image_url.url`.
* Remote image URLs, file content parts, and audio content parts are rejected before preflight.

## Related Pages

<CardGroup cols={2}>
  <Card title="Tools" icon="toolbox" href="/openai/tools">
    Understand OpenAI tool filtering.
  </Card>

  <Card title="Tool results" icon="file-shield" href="/openai/tool-results">
    Screen tool output before continuation.
  </Card>
</CardGroup>
