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

# OpenAI integration

> Integrate with the OpenAI LLM using LangChain Python.

<Warning>
  **You are currently on a page documenting the use of OpenAI text completion models. The latest and most popular OpenAI models are [chat completion models](/oss/python/langchain/models).**

  Unless you are specifically using `gpt-3.5-turbo-instruct`, you are probably looking for [this page instead](/oss/python/integrations/chat/openai/).
</Warning>

[OpenAI](https://platform.openai.com/docs/introduction) offers a spectrum of models with different levels of power suitable for different tasks.

This example goes over how to use LangChain to interact with OpenAI [models](https://platform.openai.com/docs/models)

## Overview

### Integration details

| Class                                                                                               | Package                                                                       | Local | Serializable | [JS support](https://js.langchain.com/docs/integrations/chat/openai) |                                             Downloads                                             |                                             Version                                            |
| :-------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------- | :---: | :----------: | :------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------: |
| [`ChatOpenAI`](https://reference.langchain.com/python/langchain-openai/chat_models/base/ChatOpenAI) | [`langchain-openai`](https://reference.langchain.com/python/langchain-openai) |   ❌   |     beta     |                                   ✅                                  | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-openai?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-openai?style=flat-square\&label=%20) |

## Setup

To access OpenAI models you'll need to create an OpenAI account, get an API key, and install the `langchain-openai` integration package.

### Credentials

Head to [platform.openai.com](https://platform.openai.com) to sign up to OpenAI and generate an API key. Once you've done this set the OPENAI\_API\_KEY environment variable:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if "OPENAI_API_KEY" not in os.environ:
    os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")
```

If you're routing requests through a proxy or service emulator, you can set the base URL via env var instead of passing `base_url`. Resolution order (first match wins):

1. Explicit `base_url` (or `openai_api_base`) kwarg.
2. `OPENAI_API_BASE` — read by LangChain at init.
3. `OPENAI_BASE_URL` — read by the underlying `openai` SDK client.

To enable automated tracing of your model calls, set your [LangSmith](/langsmith/observability) API key:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### Installation

The LangChain OpenAI integration lives in the `langchain-openai` package:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-openai
```

Should you need to specify your organization ID, you can use the following cell. However, it is not required if you are only part of a single organization or intend to use your default organization. You can check your default organization on the [OpenAI API keys page](https://platform.openai.com/account/api-keys).

To specify your organization, you can use this:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
OPENAI_ORGANIZATION = getpass()

os.environ["OPENAI_ORGANIZATION"] = OPENAI_ORGANIZATION
```

## Instantiation

Now we can instantiate our model object and generate chat completions:

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_openai import OpenAI

llm = OpenAI()
```

## Invocation

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
llm.invoke("Hello how are you?")
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'\n\nI am an AI and do not have emotions like humans do, so I am always functioning at my optimal level. Thank you for asking! How can I assist you today?'
```

## Chaining

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_core.prompts import PromptTemplate

prompt = PromptTemplate.from_template("How to say {input} in {output_language}:\n")

chain = prompt | llm
chain.invoke(
    {
        "output_language": "German",
        "input": "I love programming.",
    }
)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'\nIch liebe Programmieren.'
```

## Using a proxy

If you are behind an explicit proxy, you can specify the http\_client to pass through

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install httpx

import httpx

openai = OpenAI(
    model_name="gpt-3.5-turbo-instruct",
    http_client=httpx.Client(proxies="http://proxy.yourcompany.com:8080"),
)
```

***

## API reference

For detailed documentation of all [`OpenAI`](https://reference.langchain.com/python/langchain-openai/llms/base/OpenAI) llm features and configurations head to the API reference: [reference.langchain.com/python/langchain-openai/llms/base/OpenAI](https://reference.langchain.com/python/langchain-openai/llms/base/OpenAI)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/llms/openai.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
