# LangChain Python

Use `ChatOpenAI` from `langchain-openai` for Aurora's standard Chat Completions
interface. Supply Aurora's base URL explicitly and disable Responses API routing.

> Available Inference endpoint: `https://ai.aur.lu/v1` (Aurora).

## Install and prepare

Use a supported Python version in a virtual environment:

```bash
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade langchain-openai
export AURORA_API_TOKEN="<your-token>"
```

On Windows, activate with `.venv\Scripts\Activate.ps1` in PowerShell. See
[Getting started](https://docs.aur.lu/docs/inference/getting-started) to obtain the token.

## Invoke the chat model

Save as `aurora_langchain.py`:

```python
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="deepseek/deepseek-v4-flash",
    base_url="https://ai.aur.lu/v1",
    api_key=os.environ["AURORA_API_TOKEN"],
    use_responses_api=False,
    stream_usage=True,
)

response = llm.invoke([
    ("system", "You are a concise technical writing assistant."),
    ("human", "Explain what an inference API does in one sentence."),
])
print(response.content)
print(response.usage_metadata)
```

```bash
python aurora_langchain.py
```

Expect an explanation and usage metadata. The model ID is sent as-is, including
its author prefix. The base URL includes `/v1` and excludes `/chat/completions`.

## Stream a response

Replace the invocation and print statements with:

```python
for chunk in llm.stream("Explain streaming in three sentences."):
    if isinstance(chunk.content, str):
        print(chunk.content, end="", flush=True)
    if chunk.usage_metadata:
        print("\nUsage:", chunk.usage_metadata)
print()
```

For an async application, use `await llm.ainvoke(...)` or `async for` over
`llm.astream(...)` with the same configuration.

## Compatibility and troubleshooting

- **Responses-only features:** Do not attach OpenAI-hosted web search, file
  search, or code-interpreter tools. Local function tools require model tool
  support and a client-side execution loop.
- **Reasoning metadata:** `ChatOpenAI` targets standard OpenAI response fields.
  LangChain does not guarantee preservation of provider-specific fields such
  as `reasoning_content`. This recipe covers text and standard usage; evaluate
  metadata handling before building reasoning-dependent agent loops.
- **Wrong credentials or endpoint:** Pass `api_key` and `base_url` explicitly
  as shown instead of depending on unrelated OpenAI environment variables.
- **Structured-output errors:** Check the model's schema support and explicitly
  choose a compatible structured-output method rather than assuming defaults.

See [shared troubleshooting](https://docs.aur.lu/docs/inference/integrations#troubleshooting) and the
[LangChain ChatOpenAI reference](https://docs.langchain.com/oss/python/integrations/chat/openai).
