# wrapOpenAI

> **Function** in `langsmith`

📖 [View in docs](https://reference.langchain.com/javascript/langsmith/wrappers/wrapOpenAI)

Wraps an OpenAI client's completion methods, enabling automatic LangSmith
tracing. Method signatures are unchanged, with the exception that you can pass
an additional and optional "langsmithExtra" field within the second parameter.

## Signature

```javascript
wrapOpenAI<T extends OpenAIType>(openai: T, options?: Partial<RunTreeConfig>): PatchedOpenAIClient<T>
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `openai` | `T` | Yes | An OpenAI client instance. |
| `options` | `Partial<RunTreeConfig>` | No | LangSmith options. |

## Returns

`PatchedOpenAIClient<T>`

## Examples

```ts
import { OpenAI } from "openai";
import { wrapOpenAI } from "langsmith/wrappers/openai";

const patchedClient = wrapOpenAI(new OpenAI());

const patchedStream = await patchedClient.chat.completions.create(
  {
    messages: [{ role: "user", content: `Say 'foo'` }],
    model: "gpt-4.1-mini",
    stream: true,
  },
  {
    langsmithExtra: {
      metadata: {
        additional_data: "bar",
      },
    },
  },
);
```

---

[View source on GitHub](https://github.com/langchain-ai/langsmith-sdk/blob/aadfe00ed2cf31808d14a32646e73030a9eeb0b5/js/src/wrappers/openai.ts#L419)