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.
from openai import OpenAIfrom obiguard import createHeaders, OBIGUARD_GATEWAY_URLclient = OpenAI( api_key="sk-***", # Your OpenAI API key base_url=OBIGUARD_GATEWAY_URL, default_headers=createHeaders( obiguard_api_key="sk-obg***", # Your Obiguard API key ))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)
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.
Obiguard supports OpenAI’s new “developer” role in chat completions.
Starting with o1 models, the developer role replaces the previous system role.
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.
Copy
from obiguard import Obiguardclient = 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)
Copy
from obiguard import Obiguardclient = 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)
Copy
from openai import OpenAIfrom obiguard import OBIGUARD_GATEWAY_URL, createHeadersclient = OpenAI( api_key="sk-***", # Your OpenAI API key base_url=OBIGUARD_GATEWAY_URL, default_headers=createHeaders( provider="openai", 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)
The Responses API offers a more adaptable framework for creating agentic applications with integrated tools that
run automatically.
You can also stream responses from the Responses API:
Copy
response = client.responses.create( model="gpt-4.1", instructions="You are a helpful assistant.", input="Hello!", stream=True)for event in response: print(event)
Copy
response = client.responses.create( model="gpt-4.1", instructions="You are a helpful assistant.", input="Hello!", stream=True)for event in response: print(event)
Copy
response = client.responses.create( model="gpt-4.1", instructions="You are a helpful assistant.", input="Hello!", stream=True)for event in response: print(event)
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.
The Responses API also supports function calling with the same powerful capabilities:
Copy
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"] } }]response = client.responses.create( model="gpt-4.1", tools=tools, input="What is the weather like in Boston today?", tool_choice="auto")print(response)
Copy
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"] } }]response = client.responses.create( model="gpt-4.1", tools=tools, input="What is the weather like in Boston today?", tool_choice="auto")print(response)
Copy
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"] } }]response = client.responses.create( model="gpt-4.1", tools=tools, input="What is the weather like in Boston today?", tool_choice="auto")print(response)
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.
Copy
# Define the OpenAI client as shown aboveimage = openai.images.generate( model="dall-e-3", prompt="Lucy in the sky with diamonds", size="1024x1024")
Copy
# Define the OpenAI client as shown aboveimage = openai.images.generate( model="dall-e-3", prompt="Lucy in the sky with diamonds", size="1024x1024")
File search enables quick retrieval from your knowledge base across multiple file types:
Copy
response = obiguard.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)
Copy
response = obiguard.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)
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.
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.
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,
Defining a guardrail policy
Generating your virtual key for the guardrail policy
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.