IntentLayerDocsDashboard ↗
Docs/Integrations
Integrations

OpenAI SDK wrapper

Python decorated Responses API example and TypeScript Chat Completions proxy with opt-in suggestion interception.

Repository contains two paths: generic Python decorator around Responses API call and TypeScript proxy for OpenAI-compatible chat.completions.create.

#Python

python
import os
import intentlayer
from openai import OpenAI

intentlayer.init(mode="observe")

@intentlayer.wrap
def chat(request: str, session_id: str) -> str:
    response = OpenAI().responses.create(
        model=os.getenv("OPENAI_MODEL", "gpt-5-mini"),
        input=request,
    )
    return response.output_text

Example defaults to mock model. Set MOCK_LLM=0 and OPENAI_API_KEY for real call.

#TypeScript

typescript
import OpenAI from "openai";
import { IntentLayer } from "intentlayer";
import {
  isIntentLayerSuggestion,
  wrapOpenAI,
} from "intentlayer/openai";

const il = new IntentLayer({ mode: "suggest" });
const openai = wrapOpenAI(new OpenAI(), il, {
  sessionId: "session-5",
  interceptSuggestions: true,
});

const result = await openai.chat.completions.create({
  model: "gpt-4.1-mini",
  messages: [{ role: "user", content: "cancel it" }],
});

if (isIntentLayerSuggestion(result)) {
  console.log(result.questions);
}

Adapter extracts last user text. Missing user text passes through. Suggestion interception requires suggest mode, explicit interceptSuggestions: true, score at optional threshold (default 0.55), and nonempty questions. Observe and auto always pass model call through.

#Compatible surface

Works with any object exposing chat.completions.create; proxy preserves other properties. Streaming is passed to original client when not intercepted.

Source truthexamples/openai-assistant/chat.pysdk-js/src/openai.tssdk-js/README.md