Implementation:Langchain ai Langchain BaseOpenAI LLM
| Knowledge Sources | |
|---|---|
| Domains | LLM, OpenAI |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
BaseOpenAI and OpenAI are LangChain LLM classes that provide text completion via the OpenAI Completions API.
Description
The base.py file in the langchain-openai partner package defines two classes: BaseOpenAI (the base implementation) and OpenAI (the concrete, serializable subclass). BaseOpenAI extends BaseLLM from langchain-core and wraps the openai.OpenAI and openai.AsyncOpenAI clients to provide text completions (non-chat) using models like gpt-3.5-turbo-instruct. The class supports batch processing of prompts, streaming output, synchronous and asynchronous generation, token counting via tiktoken, configurable sampling parameters (temperature, top_p, frequency_penalty, presence_penalty, logit_bias, seed, logprobs), and automatic prompt batching based on configurable batch sizes. It also provides static model-to-context-size mapping and dynamic max token calculation based on prompt length.
Usage
Import OpenAI when you need text completions (non-chat) from OpenAI models. Use BaseOpenAI as a base class for custom OpenAI-compatible LLM implementations.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/partners/openai/langchain_openai/llms/base.py - Lines: 1-872
Signature
class BaseOpenAI(BaseLLM):
client: Any = Field(default=None, exclude=True)
async_client: Any = Field(default=None, exclude=True)
model_name: str = Field(default="gpt-3.5-turbo-instruct", alias="model")
temperature: float = 0.7
max_tokens: int = 256
top_p: float = 1
frequency_penalty: float = 0
presence_penalty: float = 0
n: int = 1
best_of: int = 1
model_kwargs: dict[str, Any] = Field(default_factory=dict)
openai_api_key: SecretStr | None | Callable[[], str] = Field(alias="api_key", ...)
openai_api_base: str | None = Field(alias="base_url", ...)
openai_organization: str | None = Field(alias="organization", ...)
openai_proxy: str | None = Field(...)
batch_size: int = 20
request_timeout: float | tuple[float, float] | Any | None = Field(default=None, alias="timeout")
logit_bias: dict[str, float] | None = None
max_retries: int = 2
seed: int | None = None
logprobs: int | None = None
streaming: bool = False
allowed_special: Literal["all"] | set[str] = set()
disallowed_special: Literal["all"] | Collection[str] = "all"
tiktoken_model_name: str | None = None
default_headers: Mapping[str, str] | None = None
default_query: Mapping[str, object] | None = None
http_client: Any | None = None
http_async_client: Any | None = None
extra_body: Mapping[str, Any] | None = None
class OpenAI(BaseOpenAI):
...
Import
from langchain_openai import OpenAI
# or for the base class:
from langchain_openai.llms.base import BaseOpenAI
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model_name | str | No | OpenAI model name. Defaults to "gpt-3.5-turbo-instruct". Alias: model.
|
| openai_api_key | SecretStr or Callable or None | No | API key. Inferred from OPENAI_API_KEY env var. Alias: api_key.
|
| openai_api_base | str or None | No | Base URL for API requests. Alias: base_url.
|
| openai_organization | str or None | No | Organization ID. Inferred from OPENAI_ORG_ID env var. Alias: organization.
|
| temperature | float | No | Sampling temperature. Defaults to 0.7. |
| max_tokens | int | No | Maximum tokens to generate. Defaults to 256. -1 for max possible.
|
| top_p | float | No | Nucleus sampling parameter. Defaults to 1. |
| frequency_penalty | float | No | Frequency penalty for repeated tokens. Defaults to 0. |
| presence_penalty | float | No | Presence penalty for repeated tokens. Defaults to 0. |
| n | int | No | Number of completions to generate per prompt. Defaults to 1. |
| best_of | int | No | Server-side generation count, returns best. Defaults to 1. |
| batch_size | int | No | Batch size for multiple prompts. Defaults to 20. |
| seed | int or None | No | Seed for deterministic generation. |
| logprobs | int or None | No | Number of most likely tokens to return log probabilities for. |
| streaming | bool | No | Whether to stream results. Defaults to False. |
| max_retries | int | No | Maximum request retries. Defaults to 2. |
| request_timeout | float or tuple or None | No | Request timeout. Alias: timeout.
|
Outputs
| Name | Type | Description |
|---|---|---|
| LLMResult | LLMResult | Contains lists of Generation objects with generated text, finish reason, logprobs, token usage, and model name.
|
| GenerationChunk | Iterator[GenerationChunk] | When streaming, yields chunks of generated text with finish reason and logprobs. |
Key Methods
get_token_ids
Uses tiktoken to tokenize text for the specified model, supporting custom allowed/disallowed special tokens.
modelname_to_contextsize
Static method mapping OpenAI model names to their maximum context window sizes. Supports fine-tuned model name parsing.
max_tokens_for_prompt
Calculates the maximum number of tokens that can be generated for a given prompt based on the model's context size.
get_sub_prompts
Splits a list of prompts into batches of size batch_size and handles the max_tokens=-1 case.
Usage Examples
Basic Usage
from langchain_openai import OpenAI
model = OpenAI(
model="gpt-3.5-turbo-instruct",
temperature=0,
max_retries=2,
)
response = model.invoke("The meaning of life is ")
print(response)
Streaming
from langchain_openai import OpenAI
model = OpenAI(model="gpt-3.5-turbo-instruct", streaming=True)
for chunk in model.stream("The meaning of life is "):
print(chunk, end="")
Async Usage
from langchain_openai import OpenAI
model = OpenAI(model="gpt-3.5-turbo-instruct")
response = await model.ainvoke("The meaning of life is ")
print(response)