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

# LangChain (OpenAI)

> Use LangChain's ChatOpenAI with PipeLLM's OpenAI-compatible routes

For authentication and model list, see [OpenAI Overview](/api-reference/openai/overview).

<Note>
  This page is a framework integration guide. For the raw HTTP endpoint
  reference, see [Chat Completions](/api-reference/openai/chat-completions) or
  [Responses](/api-reference/openai/responses).
</Note>

## Installation

```bash theme={null}
pip install langchain-openai
```

## Code Examples

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import os
    from langchain_openai import ChatOpenAI

    llm = ChatOpenAI(
      model='gpt-4.1',
      api_key=os.getenv('PIPELLM_API_KEY'),
      base_url='https://api.pipellm.ai/v1'
    )

    response = llm.invoke([
      ('user', 'Why is the sky blue?')
    ])

    print(response.content)
    ```
  </Tab>

  <Tab title="Node.js">
    ```javascript theme={null}
    import { ChatOpenAI } from '@langchain/openai';

    const llm = new ChatOpenAI({
      model: 'gpt-4.1',
      apiKey: process.env.PIPELLM_API_KEY,
      configuration: {
        baseURL: 'https://api.pipellm.ai/v1'
      }
    });

    const response = await llm.invoke([
      { role: 'user', content: 'Why is the sky blue?' }
    ]);

    console.log(response.content);
    ```
  </Tab>
</Tabs>

## Streaming

```python theme={null}
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
  model='gpt-4.1',
  api_key=os.getenv('PIPELLM_API_KEY'),
  base_url='https://api.pipellm.ai/v1',
  streaming=True
)

for chunk in llm.stream('Tell me a story'):
  print(chunk.content, end='', flush=True)
```

## Function Calling

```python theme={null}
import os
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool

@tool
def get_weather(location: str) -> str:
  """Get the weather for a location."""
  return f"Weather in {location}: 22°C, Sunny"

llm = ChatOpenAI(
  model='gpt-4.1',
  api_key=os.getenv('PIPELLM_API_KEY'),
  base_url='https://api.pipellm.ai/v1'
)

llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("What's the weather in Tokyo?")

print(response.tool_calls)
```

## Related Docs

<Columns cols={3}>
  <Card title="OpenAI Overview" icon="bolt" href="/api-reference/openai/overview">
    Headers, models, and native routes
  </Card>

  <Card title="Chat Completions" icon="comments" href="/api-reference/openai/chat-completions">
    Raw endpoint reference for `POST /v1/chat/completions`
  </Card>

  <Card title="Developer Tools Overview" icon="terminal" href="/integrations/overview">
    More tools and framework integrations on PipeLLM
  </Card>
</Columns>
