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

# OpenAI

> Discover how to integrate OpenAI with Obiguard for seamless completions, prompt management, and advanced features like streaming, function calling, and fine-tuning.

Obiguard  has native integrations with OpenAI SDKs for Python, and its REST APIs.

<Note>
  Provider slug: `openai`
</Note>

## Using the Obiguard Gateway

To integrate the Obiguard gateway with OpenAI,

* Set the `baseURL` to the Obiguard Gateway URL
* Include Obiguard-specific headers such as `provider`, `obiguardApiKey`, and others.

Here's how to apply it to a **chat completion** request:

<Tabs>
  <Tab title="Python SDK">
    Install the Obiguard SDK with pip

    ```sh theme={null}
    pip install obiguard
    ```

    <CodeGroup>
      ```py Chat Completions theme={null}
      from obiguard import Obiguard

      client = Obiguard(
        obiguard_api_key="vk-obg***",  # Your Obiguard virtual key
        provider='openai',
        strict_open_ai_compliance=False
      )

      response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Hello!"}
        ]
      )

      print(response.choices[0].message)
      ```
    </CodeGroup>
  </Tab>

  <Tab title="cURL">
    <CodeGroup>
      ```sh Chat Completions theme={null}
      curl https://gateway.obiguard.ai/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "x-obiguard-api-key: $OBIGUARD_API_KEY" \
      -d '{
          "model": "gpt-4o",
          "messages": [
            {"role": "user", "content": "Hello!"}
          ]
        }'
      ```
    </CodeGroup>
  </Tab>

  <Tab title="OpenAI Python SDK">
    Install the OpenAI and Obiguard SDKs with `pip`

    ```sh theme={null}
    pip install openai obiguard
    ```

    <CodeGroup>
      ```py Chat Completions theme={null}
      from openai import OpenAI
      from obiguard import OBIGUARD_GATEWAY_URL, Obiguard

      obiguard_client = Obiguard(
        obiguard_api_key='sk-obg***',  # Your Obiguard policy group API key
        provider='openai',
      )

      openai_client = OpenAI(
        api_key="sk-***",  # Your OpenAI API key
        base_url=OBIGUARD_GATEWAY_URL,
        default_headers=obiguard_client.copy_headers()
      )

      completion = client.chat.completions.create(
        model="gpt-4o",
        messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Hello!"}
        ]
      )
      print(completion.choices[0].message)
      ```
    </CodeGroup>
  </Tab>
</Tabs>

This request will be logged automatically by Obiguard and can be viewed in your logs dashboard.
Obiguard tracks the tokens used, execution time, and cost for each request.
You can also examine detailed request and response data.

<Note>
  Obiguard supports [OpenAI's new "developer" role](https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages) in chat completions.
  Starting with o1 models, the `developer` role replaces the previous `system` role.
</Note>

### Using the Responses API

OpenAI has introduced a new Responses API that merges the capabilities of Chat Completions and Assistants APIs.
Obiguard provides full support for this API, allowing its use with both the Obiguard SDK and the OpenAI SDK.

<Tabs>
  <Tab title="Python SDK">
    ```python theme={null}
    from obiguard import Obiguard

    client = Obiguard(
      obiguard_api_key="sk-obg***",  # Your Obiguard API key
    )

    response = client.responses.create(
      model="gpt-4.1",
      input="Tell me a three sentence bedtime story about a unicorn."
    )

    print(response)
    ```
  </Tab>

  <Tab title="OpenAI Python SDK">
    ```python theme={null}
    import os

    from openai import OpenAI
    from obiguard import OBIGUARD_GATEWAY_URL, Obiguard

    obiguard_client = Obiguard(
      obiguard_api_key='sk-obg***',  # Your Obiguard policy group API key
      provider='openai',
    )

    openai_client = OpenAI(
      api_key=os.environ.get("OPENAI_API_KEY"),
      base_url=OBIGUARD_GATEWAY_URL,
      default_headers=obiguard_client.copy_headers()
    )

    response = openai_client.responses.create(
      model="gpt-4o",
      instructions="You are a coding assistant that talks like a pirate.",
      input="How do I check if a Python object is an instance of a class?",
    )

    print(response)
    ```
  </Tab>
</Tabs>

<Note>
  The Responses API offers a more adaptable framework for creating agentic applications with integrated tools that
  run automatically.
</Note>

<Card title="Remote MCP Support in Responses API" href="/gateway/remote-mcp">
  Learn how Obiguard enables Remote MCP support for OpenAI's Responses API.
</Card>

<Note>
  * The same integration approach applies to APIs for
    [completions](https://platform.openai.com/docs/guides/text-generation/completions-api),
    [embeddings](https://platform.openai.com/docs/api-reference/embeddings/create),
    [vision](https://platform.openai.com/docs/guides/vision/quick-start),
    [moderation](https://platform.openai.com/docs/api-reference/moderations/create),
    [transcription](https://platform.openai.com/docs/api-reference/audio/createTranscription),
    [translation](https://platform.openai.com/docs/api-reference/audio/createTranslation),
    [speech](https://platform.openai.com/docs/api-reference/audio/createSpeech) and
    [files](https://platform.openai.com/docs/api-reference/files/create).
</Note>

### Realtime API

Obiguard seamlessly integrates with OpenAI's Realtime API, enabling features like logging, cost tracking, and guardrails.

<Card title="Realtime API" href="/gateway/realtime-api" />

### Streaming Responses

Obiguard supports streaming responses through Server-Sent Events (SSE).

<Tabs>
  <Tab title="OpenAI Python SDK">
    ```python theme={null}
    import os
    from openai import OpenAI
    from obiguard import OBIGUARD_GATEWAY_URL, Obiguard

    obiguard_client = Obiguard(
      obiguard_api_key='sk-obg***',  # Your Obiguard policy group API key
      provider='openai',
    )

    openai_client = OpenAI(
      api_key=os.environ.get("OPENAI_API_KEY"),
      base_url=OBIGUARD_GATEWAY_URL,
      default_headers=obiguard_client.copy_headers()
    )

    stream = openai_client.responses.create(
      model="gpt-4o",
      input="Write a one-sentence bedtime story about a unicorn.",
      stream=True,
    )

    for event in stream:
      print(event)
    ```
  </Tab>
</Tabs>

#### Streaming with the Responses API

You can also stream responses from the Responses API:

<Tabs>
  <Tab title="Python SDK">
    ```python theme={null}
    response = client.responses.create(
      model="gpt-4.1",
      instructions="You are a helpful assistant.",
      input="Hello!",
      stream=True
    )

    for event in response:
      print(event)
    ```
  </Tab>

  <Tab title="OpenAI Python SDK">
    ```python theme={null}
    response = client.responses.create(
      model="gpt-4.1",
      instructions="You are a helpful assistant.",
      input="Hello!",
      stream=True
    )

    for event in response:
      print(event)
    ```
  </Tab>
</Tabs>

### Vision Models Support

Obiguard's multimodal Gateway provides full compatibility with OpenAI vision models. Refer to this guide for additional details:

<Info>
  [Vision](/gateway/multimodal-capabilities/vision)
</Info>

#### Using Vision Models with the Responses API

The Responses API also enables processing images alongside text:

<Tabs>
  <Tab title="Python SDK">
    ```python theme={null}
    from obiguard import Obiguard

    prompt = "What is in this image?"
    img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/2023_06_08_Raccoon1.jpg/1599px-2023_06_08_Raccoon1.jpg"

    obiguard_client = Obiguard(
      obiguard_api_key='vk-obg***',  # Your Obiguard virtual key proxy to OpenAI
      provider='openai',
    )

    response = obiguard_client.responses.create(
      model="gpt-4o-mini",
      input=[
        {
          "role": "user",
          "content": [
            {"type": "input_text", "text": prompt},
            {"type": "input_image", "image_url": f"{img_url}"},
          ],
        }
      ],
    )

    print(response)
    ```
  </Tab>

  <Tab title="OpenAI Python SDK">
    ```python theme={null}
    import os
    from openai import OpenAI
    from obiguard import OBIGUARD_GATEWAY_URL, Obiguard

    prompt = "What is in this image?"
    img_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/2023_06_08_Raccoon1.jpg/1599px-2023_06_08_Raccoon1.jpg"

    obiguard_client = Obiguard(
      obiguard_api_key='sk-obg***',  # Your Obiguard policy group API key
      provider='openai',
    )

    openai_client = OpenAI(
      api_key=os.environ.get("OPENAI_API_KEY"),
      base_url=OBIGUARD_GATEWAY_URL,
      default_headers=obiguard_client.copy_headers()
    )

    response = openai_client.responses.create(
      model="gpt-4o-mini",
      input=[
        {
          "role": "user",
          "content": [
            {"type": "input_text", "text": prompt},
            {"type": "input_image", "image_url": f"{img_url}"},
          ],
        }
      ],
    )

    print(response)
    ```
  </Tab>
</Tabs>

### Function Calling

Function calls within your OpenAI or Obiguard SDK operations remain standard. These logs will appear in Obiguard, highlighting the utilized functions and their outputs.

Additionally, you can define functions within your prompts and invoke the `obiguard.prompts.completions.create` method as above.

#### Function Calling with the Responses API

The Responses API also supports function calling with the same powerful capabilities:

<Tabs>
  <Tab title="Python SDK">
    ```python theme={null}
    tools = [
      {
        "type": "function",
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
          "location": {
            "type": "string",
            "description": "The city and state, e.g. San Francisco, CA"
          },
            "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
          },
          "required": ["location", "unit"]
        }
      }
    ]

    obiguard_client = Obiguard(
      obiguard_api_key='vk-obg***',  # Your Obiguard virtual key proxy to OpenAI
      provider='openai',
    )

    response = obiguard_client.responses.create(
      model="gpt-4.1",
      tools=tools,
      input="What is the weather like in Boston today?",
      tool_choice="auto"
    )

    print(response)
    ```
  </Tab>

  <Tab title="OpenAI Python SDK">
    ```python theme={null}
    tools = [
      {
        "type": "function",
        "name": "get_current_weather",
        "description": "Get the current weather in a given location",
        "parameters": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            },
            "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
          },
          "required": ["location", "unit"]
        }
      }
    ]

    obiguard_client = Obiguard(
      obiguard_api_key='sk-obg***',  # Your Obiguard policy group API key
      provider='openai',
    )

    openai_client = OpenAI(
      api_key=os.environ.get("OPENAI_API_KEY"),
      base_url=OBIGUARD_GATEWAY_URL,
      default_headers=obiguard_client.copy_headers()
    )

    response = openai_client.responses.create(
      model="gpt-4.1",
      tools=tools,
      input="What is the weather like in Boston today?",
      tool_choice="auto"
    )

    print(response)
    ```
  </Tab>
</Tabs>

### Image Generation

Obiguard supports multiple modalities for OpenAI and you can make image generation requests through Obiguard's AI Gateway the same way as making completion calls.

<Tabs>
  <Tab title="OpenAI Python SDK">
    ```python theme={null}
    import os
    from openai import OpenAI
    from obiguard import OBIGUARD_GATEWAY_URL, Obiguard

    # Define the OpenAI client as shown above
    obiguard_client = Obiguard(
      obiguard_api_key='sk-obg***',  # Your Obiguard policy group API key
      provider='openai',
    )

    openai_client = OpenAI(
      api_key=os.environ.get("OPENAI_API_KEY"),
      base_url=OBIGUARD_GATEWAY_URL,
      default_headers=obiguard_client.copy_headers()
    )

    image = openai_client.images.generate(
      model="dall-e-3",
      prompt="Lucy in the sky with diamonds",
      size="1024x1024"
    )
    ```
  </Tab>
</Tabs>

### Audio - Transcription, Translation, and Text-to-Speech

Obiguard's multimodal Gateway also supports the `audio` methods on OpenAI API. Check out the below guides for more info:

Check out the below guides for more info:

<Info>
  [Text-to-Speech](/gateway/multimodal-capabilities/text-to-speech)
</Info>

<Info>
  [Speech-to-Text](/gateway/multimodal-capabilities/speech-to-text)
</Info>

***

## Integrated Tools with Responses API

### Web Search Tool

Web search delivers accurate and clearly-cited answers from the web, using the same tool as search in ChatGPT:

<Tabs>
  <Tab title="Python SDK">
    ```python theme={null}
    response = obiguard_client.responses.create(
      model="gpt-4.1",
      tools=[{
        "type": "web_search_preview",
        "search_context_size": "medium", # Options: "high", "medium" (default), or "low"
        "user_location": {# Optional - for localized results
          "type": "approximate",
          "country": "US",
          "city": "San Francisco",
          "region": "California"
        }
      }],
      input="What was a positive news story from today?"
    )
    print(response)
    ```
  </Tab>
</Tabs>

<Note>
  **Options for `search_context_size`:**

  * `high`: Most comprehensive context, higher cost, slower response
  * `medium`: Balanced context, cost, and latency (default)
  * `low`: Minimal context, lowest cost, fastest response

  Responses include citations for URLs found in search results, with clickable references.
</Note>

### File Search Tool

File search enables quick retrieval from your knowledge base across multiple file types:

<Tabs>
  <Tab title="Python SDK">
    ```python theme={null}
    response = obiguard_client.responses.create(
      model="gpt-4.1",
      tools=[{
        "type": "file_search",
        "vector_store_ids": ["vs_1234567890"],
        "max_num_results": 20,
        "filters": {# Optional - filter by metadata
          "type": "eq",
          "key": "document_type",
          "value": "report"
        }
      }],
      input="What are the attributes of an ancient brown dragon?"
    )

    print(response)
    ```
  </Tab>
</Tabs>

<Note>
  This tool requires you to first create a vector store and upload files to it. Supports various file formats including
  PDFs, DOCXs, TXT, and more. Results include file citations in the response.
</Note>

### Enhanced Reasoning

Control the depth of model reasoning for more comprehensive analysis:

<Tabs>
  <Tab title="Python SDK">
    ```python theme={null}
    response = obiguard.responses.create(
      model="o3-mini",
      input="How much wood would a woodchuck chuck?",
      reasoning={
        "effort": "high"  # Options: "high", "medium", or "low"
      }
    )
    print(response)
    ```
  </Tab>
</Tabs>

### Computer Use Assistant

Obiguard also supports the Computer Use Assistant (CUA) tool, which helps agents control computers or virtual machines through screenshots and actions. This feature is available for select developers as a research preview on premium tiers.

<Card href="https://platform.openai.com/docs/guides/tools-computer-use?lang=python">
  Learn More about Computer use tool here
</Card>

## Managing OpenAI Projects & Organizations in Obiguard

When integrating OpenAI with Obiguard, you can specify your OpenAI organization and project IDs along with your API key.
This is particularly useful if you belong to multiple organizations or are accessing projects through a legacy user API key.

Specifying the organization and project IDs helps you maintain better control over your access rules, usage, and costs.

In Obiguard, you can add your Org & Project details by,

2. Defining a guardrail policy
3. Generating your virtual key for the guardrail policy
4. Passing details in a request

Let's explore each method in more detail.

### Using Virtual Keys

When selecting OpenAI from the dropdown menu while creating a virtual key,
Obiguard automatically displays optional fields for the organization ID and project ID alongside the API key field.

[Get your OpenAI API key from here](https://platform.openai.com/api-keys), then add it to Obiguard to create the virtual key that can be used throughout Obiguard.

### While Making a Request

You can also pass your organization and project details directly when making a request using curl, the OpenAI SDK, or the Obiguard SDK.

<Tabs>
  <Tab title="OpenAI Python SDK">
    ```python theme={null}
    from openai import OpenAI
    from obiguard import OBIGUARD_GATEWAY_URL, Obiguard

    obiguard_client = Obiguard(
      obiguard_api_key='sk-obg***',  # Your Obiguard policy group API key
      provider='openai',
    )

    openai_client = OpenAI(
      api_key=os.environ.get("OPENAI_API_KEY"),
      base_url=OBIGUARD_GATEWAY_URL,
      default_headers=obiguard_client.copy_headers()
    )

    chat_complete = openai_client.chat.completions.create(
      model="gpt-4o",
      messages=[{"role": "user", "content": "Say this is a test"}],
    )
    )

    print(chat_complete.choices[0].message.content)
    ```
  </Tab>

  <Tab title="cURL">
    ```sh theme={null}
    curl https://gateway.obiguard.ai/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer $OPENAI_API_KEY" \
      -H "x-obiguard-openai-organization: org-xxxxxxx" \
      -H "x-obiguard-openai-project: proj_xxxxxxx" \
      -H "x-obiguard-api-key: OBIGUARD_API_KEY" \
      -H "x-obiguard-provider: openai" \
      -d '{
        "model": "gpt-4o",
        "messages": [{"role": "user","content": "Hello!"}]
      }'
    ```
  </Tab>

  <Tab title="Obiguard Python SDK">
    ```python theme={null}
    from obiguard import Obiguard

    client = Obiguard(
      obiguard_api_key="vk-obg***",  # Your Obiguard virtual key
      provider='openai',
      strict_open_ai_compliance=False
    )

    chat_complete = client.chat.completions.create(
      model="gpt-4o",
      messages=[{"role": "user", "content": "Say this is a test"}],
    )

    print(chat_complete.choices[0].message.content)
    ```
  </Tab>
</Tabs>
