LangChain Python integration

Call ScriptEngine from LangChain.

Use LangChain’s OpenAI-compatible ChatOpenAI adapter with ScriptEngine, select a model returned by discovery, and invoke a plain text message before adding chains or tools.

  • ScriptEngine surface rechecked August 12, 2026
  • Python and langchain-openai
  • Plain chat invocation first

Compatibility boundary

Use the stable chat path before adding LangChain features

LangChain’s official OpenAI integration accepts an explicit API key and base_url, which makes it suitable for an OpenAI-compatible endpoint. This page uses only the ScriptEngine surface currently verified with bearer authentication, authenticated model discovery, and /v1/chat/completions.

LangChain can route some advanced features through the Responses API and can attach tools, structured output, multimodal content, or streaming usage metadata. A successful basic invocation does not prove those capabilities on every ScriptEngine model. Keep this tutorial’s first test to one text message and expand only after a model-specific request has been tested.

The authoritative client reference is LangChain’s ChatOpenAI integration documentation. This guide deliberately avoids OpenAI-only parameters that ScriptEngine has not published as supported.

Requirements

Install the adapter and discover a current model

  1. Use Python 3.9 or newer in a virtual environment.
  2. Install the maintained adapter with pip install -U langchain-openai.
  3. Create a private ScriptEngine key and store it in SCRIPTENGINE_API_KEY.
  4. Use the same key to discover a model ID; do not hard-code a stale catalog value.
Install and discover
python -m venv .venv
source .venv/bin/activate
pip install -U langchain-openai

export SCRIPTENGINE_API_KEY="YOUR_PRIVATE_KEY"
curl https://scriptengine.org/v1/models \
  -H "Authorization: Bearer $SCRIPTENGINE_API_KEY"

Choose a text model from the JSON response and use its exact value as MODEL_ID_FROM_DISCOVERY. The public model catalog is a rate reference; the authenticated response is the final availability check.

Python configuration

Instantiate ChatOpenAI with ScriptEngine

LangChain reads the API key from the environment, while the explicit base URL keeps the routing decision visible in your application. Setting stream_usage=False avoids assuming that a non-OpenAI endpoint emits OpenAI’s optional streaming usage metadata.

scriptengine_chat.py
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="MODEL_ID_FROM_DISCOVERY",
    api_key=os.environ["SCRIPTENGINE_API_KEY"],
    base_url="https://scriptengine.org/v1",
    stream_usage=False,
)

response = llm.invoke("Reply with exactly: LangChain connected")
print(response.content)

Keep the first prompt short and avoid sending private repository contents until the connection, model, and retention expectations are understood. If your installed LangChain version does not accept stream_usage, remove that optional line and keep the plain invocation.

Verification

Separate API errors from chain errors

Run a direct request first. This isolates credentials and model selection from LangChain callbacks, memory, retrievers, and tools.

Direct chat-completions check
curl https://scriptengine.org/v1/chat/completions \
  -H "Authorization: Bearer $SCRIPTENGINE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"MODEL_ID_FROM_DISCOVERY","messages":[{"role":"user","content":"Reply with exactly: ScriptEngine connected"}]}'

After the direct request succeeds, run the Python snippet. A normal AIMessage with text content confirms the basic adapter path. Only then add a prompt template or a simple chain; delay tools, agents, image input, embeddings, and Responses-specific options until each has its own compatibility test.

Troubleshooting

Fix common LangChain connection failures

401 or missing API key
Check that SCRIPTENGINE_API_KEY is exported in the same shell that starts Python. Test that exact value with /v1/models.
404 or URL duplication
Set base_url to https://scriptengine.org/v1. Do not include /chat/completions; the adapter adds the path.
404 or model not found
Refresh model discovery and copy the exact returned ID. Model names from another provider or a previous catalog are not guaranteed to exist.
Tools or structured output fail
Keep the plain text invocation as the known-good baseline. LangChain features can change the request protocol; do not infer support from the fact that the Python class exposes the option.

Cost and safety

Measure a chain before scaling it

Retrievers, agents, and long prompts can multiply token use. Start with a small prepaid balance, inspect the current model rates, and monitor usage in the ScriptEngine workspace. The dated 86% reference maximum is not a blanket discount for all LangChain workloads.

Keep keys outside source control and logs. ScriptEngine currently publishes no uptime or response-time SLA. For important applications, configure timeouts, bounded retries, error handling, and your own fallback provider after testing the request semantics.

Sources and next steps

Follow the client’s current API reference

This page was checked August 4, 2026 against LangChain’s current ChatOpenAI documentation and ScriptEngine’s published endpoint contract. Re-check both before upgrading dependencies or enabling advanced features.

Next check

Read the ScriptEngine API contract

Confirm bearer authentication, model discovery, and chat completions before adding chains or agents.

Open API docs