Skip to main content

Use LangChain

Use Python's ChatOpenAI for Primex Chat Completions. Select an eligible model in LangChain Setup. Choose features that match the selected Primex offering and protocol.

Install and run​

Shell
python -m pip install langchain-openai
export PRIMEX_API_KEY=YOUR_API_KEY
export PRIMEX_CHAT_MODEL=YOUR_LIVE_CHAT_MODEL

Save as example.py and run python example.py:

Python
import os
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
model=os.environ["PRIMEX_CHAT_MODEL"],
api_key=os.environ["PRIMEX_API_KEY"],
base_url="https://gateway.primexos.com/v1",
use_responses_api=False,
stream_usage=False,
timeout=60, max_retries=0,
)
print(model.invoke("Hello from Primex").content)

The text answer prints in the terminal. This explicitly selects Chat Completions and disables automatic retries. To stream, replace the final line with:

Python
for chunk in model.stream("Hello from Primex"):
print(chunk.content, end="", flush=True)
print()

Scope, tools, and data​

ChatOpenAI targets the official OpenAI specifications. Provider-specific fields such as reasoning_content or reasoning_details may not be preserved. Do not assume that a text reply qualifies reasoning, structured output, multimodal inputs, Responses state, built-in tools, or a LangGraph agent.

A tool workflow needs a supported model, a validated tool call, execution by your application, and the matching result continuation. See the complete tool loop. For retrieval, use the RAG recipe; reranking is a separate native service rather than a ChatOpenAI option.

LangSmith tracing is optional and sends trace data to another service. Review its data settings before enabling it; this guide does not require tracing. Use Primex Usage for recorded requests and charges.

Common fixes​

  • 401: check the process environment and the explicit api_key argument.
  • 404/wrong API: use /v1, the exact model ID, and use_responses_api=False.
  • Missing reasoning data: use an adapter that preserves the required provider fields and verify it separately; do not reconstruct them from ordinary text.
  • Timeout/402/429: inspect Errors & limits and Usage before retrying.

To disconnect, remove the client configuration and revoke unused keys.

See the official provider reference for additional client settings.