Skip to main content
您可以在 OpenAI Platform 文档中找到 OpenAI 最新模型的相关信息,包括费用、上下文窗口和支持的输入类型。
API 参考有关所有功能和配置选项的详细文档,请参阅 ChatOpenAI API 参考。
API 范围ChatOpenAI 仅针对官方 OpenAI API 规范。来自第三方提供商的非标准响应字段(如 reasoning_contentreasoningreasoning_details不会被提取或保留。如果您使用的是扩展了聊天补全或 Responses 格式的提供商,例如 OpenRouterLiteLLMvLLMDeepSeek,请改用提供商专用的包。详情请参阅聊天补全 API 兼容性

概述

集成详情

可序列化JS/TS 支持下载量最新版本
ChatOpenAIlangchain-openaibeta(npm)Downloads per monthPyPI - Latest version

模型功能

工具调用结构化输出图像输入音频输入视频输入Token 级流式输出原生异步Token 用量对数概率

安装配置

要访问 OpenAI 模型,您需要安装 langchain-openai 集成包并获取 OpenAI Platform API 密钥。

安装

pip install -U langchain-openai

凭据

前往 OpenAI Platform 注册并生成 API 密钥。完成后,在您的环境中设置 OPENAI_API_KEY 环境变量:
import getpass
import os

if not os.environ.get("OPENAI_API_KEY"):
    os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")
如果希望自动追踪模型调用,也可以设置您的 LangSmith API 密钥:
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"

实例化

现在可以实例化模型对象并生成响应:
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-5-nano",
    # stream_usage=True,
    # temperature=None,
    # max_tokens=None,
    # timeout=None,
    # reasoning_effort="low",
    # max_retries=2,
    # api_key="...",  # 如果希望直接传入 API 密钥
    # base_url="...",
    # organization="...",
    # 其他参数...
)
完整的可用模型参数列表,请参阅 ChatOpenAI API 参考。
Token 参数弃用说明OpenAI 于 2024 年 9 月弃用了 max_tokens,改用 max_completion_tokens。虽然 max_tokens 为了向后兼容仍受支持,但在内部会自动转换为 max_completion_tokens

调用

messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
AIMessage(content="J'adore la programmation.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 5, 'prompt_tokens': 31, 'total_tokens': 36}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_3aa7262c27', 'finish_reason': 'stop', 'logprobs': None}, id='run-63219b22-03e3-4561-8cc4-78b7c7c3a3ca-0', usage_metadata={'input_tokens': 31, 'output_tokens': 5, 'total_tokens': 36})
print(ai_msg.text)
J'adore la programmation.

流式输出的用量元数据

OpenAI 的聊天补全 API 默认不在流式传输时返回 token 用量统计(请参阅此处的 API 参考)。 要在使用 ChatOpenAIAzureChatOpenAI 进行流式传输时获取 token 计数,请将 stream_usage=True 设置为初始化参数或在调用时传入:
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4.1-mini", stream_usage=True)

与 Azure OpenAI 配合使用

Azure OpenAI v1 API 支持langchain-openai>=1.0.1 开始,ChatOpenAI 可以直接使用新的 v1 API 与 Azure OpenAI 端点配合使用。这提供了一种统一的方式来使用托管在 OpenAI 或 Azure 上的 OpenAI 模型。如需使用传统的 Azure 专用实现,请继续使用 AzureChatOpenAI
要将 ChatOpenAI 与 Azure OpenAI 配合使用,请将 base_url 设置为附加了 /openai/v1/ 的 Azure 端点:
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-5-mini",  # 您的 Azure 部署名称
    base_url="https://{your-resource-name}.openai.azure.com/openai/v1/",
    api_key="your-azure-api-key"
)

response = llm.invoke("Hello, how are you?")
print(response.content)
v1 API 新增了对 Microsoft Entra ID(原 Azure AD)身份验证的原生支持,支持自动 token 刷新。将 token 提供者可调用对象传递给 api_key 参数:
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from langchain_openai import ChatOpenAI

# 创建自动处理刷新的 token 提供者
token_provider = get_bearer_token_provider(
    DefaultAzureCredential(),
    "https://cognitiveservices.azure.com/.default"
)

llm = ChatOpenAI(
    model="gpt-5-mini",  # 您的 Azure 部署名称
    base_url="https://{your-resource-name}.openai.azure.com/openai/v1/",
    api_key=token_provider  # 处理 token 刷新的可调用对象
)

# 正常使用模型
messages = [
    ("system", "You are a helpful assistant."),
    ("human", "Translate 'I love programming' to French.")
]
response = llm.invoke(messages)
print(response.content)
token 提供者是一个可调用对象,可自动获取和刷新认证 token,无需手动管理 token 过期。
安装要求要使用 Microsoft Entra ID 身份验证,请安装 Azure Identity 库:
pip install azure-identity
使用异步函数时,也可以将 token 提供者可调用对象传递给 api_key 参数。此时必须从 azure.identity.aio 导入 DefaultAzureCredential:
from azure.identity.aio import DefaultAzureCredential
from langchain_openai import ChatOpenAI

credential = DefaultAzureCredential()

llm_async = ChatOpenAI(
    model="gpt-5-nano",
    api_key=credential
)

# 使用异步可调用对象时必须使用异步方法
response = await llm_async.ainvoke("Hello!")
使用异步可调用对象作为 API 密钥时,必须使用异步方法(ainvokeastream 等),同步方法将会报错。

工具调用

OpenAI 提供了工具调用(此处”工具调用”与”函数调用”可互换使用)API,允许您描述工具及其参数,并让模型返回包含待调用工具及其输入的 JSON 对象。工具调用对于构建使用工具的链和 Agent 极为有用,也是从模型获取结构化输出的通用方式。

绑定工具

使用 ChatOpenAI.bind_tools,我们可以轻松地将 Pydantic 类、字典 schema、LangChain 工具甚至函数作为工具传入模型。在底层,这些会被转换为 OpenAI 工具 schema,格式如下:
{
    "name": "...",
    "description": "...",
    "parameters": {...}  # JSONSchema
}
…并在每次模型调用时传入。
from pydantic import BaseModel, Field


class GetWeather(BaseModel):
    """Get the current weather in a given location"""

    location: str = Field(..., description="The city and state, e.g. San Francisco, CA")


llm_with_tools = llm.bind_tools([GetWeather])
ai_msg = llm_with_tools.invoke(
    "what is the weather like in San Francisco",
)
ai_msg
AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 17, 'prompt_tokens': 68, 'total_tokens': 85}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_3aa7262c27', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-1617c9b2-dda5-4120-996b-0333ed5992e2-0', tool_calls=[{'name': 'GetWeather', 'args': {'location': 'San Francisco, CA'}, 'id': 'call_o9udf3EVOWiV4Iupktpbpofk', 'type': 'tool_call'}], usage_metadata={'input_tokens': 68, 'output_tokens': 17, 'total_tokens': 85})

严格模式

需要 langchain-openai>=0.1.21
自 2024 年 8 月 6 日起,OpenAI 在工具调用时支持 strict 参数,该参数将强制模型遵守工具参数 schema。了解更多
如果 strict=True,工具定义也会被验证,且只接受 JSON schema 的子集。关键点是 schema 不能包含可选参数(即有默认值的参数)。请阅读完整文档了解支持的 schema 类型。
llm_with_tools = llm.bind_tools([GetWeather], strict=True)
ai_msg = llm_with_tools.invoke(
    "what is the weather like in San Francisco",
)
ai_msg
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_jUqhd8wzAIzInTJl72Rla8ht', 'function': {'arguments': '{"location":"San Francisco, CA"}', 'name': 'GetWeather'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 17, 'prompt_tokens': 68, 'total_tokens': 85}, 'model_name': 'gpt-4o-2024-05-13', 'system_fingerprint': 'fp_3aa7262c27', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-5e3356a9-132d-4623-8e73-dd5a898cf4a6-0', tool_calls=[{'name': 'GetWeather', 'args': {'location': 'San Francisco, CA'}, 'id': 'call_jUqhd8wzAIzInTJl72Rla8ht', 'type': 'tool_call'}], usage_metadata={'input_tokens': 68, 'output_tokens': 17, 'total_tokens': 85})

工具调用结果

注意 AIMessage 具有 tool_calls 属性,该属性以与模型提供商无关的标准化 ToolCall 格式呈现。
ai_msg.tool_calls
[{'name': 'GetWeather',
  'args': {'location': 'San Francisco, CA'},
  'id': 'call_jUqhd8wzAIzInTJl72Rla8ht',
  'type': 'tool_call'}]
更多关于绑定工具和工具调用输出的内容,请参阅工具调用文档。

自定义工具

需要 langchain-openai>=0.3.29
自定义工具支持带有任意字符串输入的工具,在预期字符串参数较长或复杂时特别有用。
from langchain_openai import ChatOpenAI, custom_tool
from langchain.agents import create_agent


@custom_tool
def execute_code(code: str) -> str:
    """Execute python code."""
    return "27"


llm = ChatOpenAI(model="gpt-5", use_responses_api=True)

agent = create_agent(llm, [execute_code])

input_message = {"role": "user", "content": "Use the tool to calculate 3^3."}
for step in agent.stream(
    {"messages": [input_message]},
    stream_mode="values",
):
    step["messages"][-1].pretty_print()
================================ Human Message =================================

Use the tool to calculate 3^3.
================================== Ai Message ==================================

[{'id': 'rs_68b7336cb72081a080da70bf5e980e4e0d6082d28f91357a', 'summary': [], 'type': 'reasoning'}, {'call_id': 'call_qyKsJ4XlGRudbIJDrXVA2nQa', 'input': 'print(3**3)', 'name': 'execute_code', 'type': 'custom_tool_call', 'id': 'ctc_68b7336f718481a0b39584cd35fbaa5d0d6082d28f91357a', 'status': 'completed'}]
Tool Calls:
  execute_code (call_qyKsJ4XlGRudbIJDrXVA2nQa)
 Call ID: call_qyKsJ4XlGRudbIJDrXVA2nQa
  Args:
    __arg1: print(3**3)
================================= Tool Message =================================
Name: execute_code

[{'type': 'custom_tool_call_output', 'output': '27'}]
================================== Ai Message ==================================

[{'type': 'text', 'text': '27', 'annotations': [], 'id': 'msg_68b73371e9e081a0927f54f88f2cd7a20d6082d28f91357a'}]
OpenAI 支持为自定义工具输入指定上下文无关文法,格式为 larkregex。详情请参阅 OpenAI 文档format 参数可以通过 @custom_tool 传入,如下所示:
from langchain_openai import ChatOpenAI, custom_tool
from langchain.agents import create_agent


grammar = """
start: expr
expr: term (SP ADD SP term)* -> add
| term
term: factor (SP MUL SP factor)* -> mul
| factor
factor: INT
SP: " "
ADD: "+"
MUL: "*"
%import common.INT
"""

format_ = {"type": "grammar", "syntax": "lark", "definition": grammar}

@custom_tool(format=format_)
def do_math(input_string: str) -> str:
    """Do a mathematical operation."""
    return "27"


llm = ChatOpenAI(model="gpt-5", use_responses_api=True)

agent = create_agent(llm, [do_math])

input_message = {"role": "user", "content": "Use the tool to calculate 3^3."}
for step in agent.stream(
    {"messages": [input_message]},
    stream_mode="values",
):
    step["messages"][-1].pretty_print()
================================ Human Message =================================

Use the tool to calculate 3^3.
================================== Ai Message ==================================

[{'id': 'rs_68b733f066a48194a41001c0cc1081760811f11b6f4bae47', 'summary': [], 'type': 'reasoning'}, {'call_id': 'call_7hTYtlTj9NgWyw8AQGqETtV9', 'input': '3 * 3 * 3', 'name': 'do_math', 'type': 'custom_tool_call', 'id': 'ctc_68b733f3a0a08194968b8338d33ad89f0811f11b6f4bae47', 'status': 'completed'}]
Tool Calls:
  do_math (call_7hTYtlTj9NgWyw8AQGqETtV9)
 Call ID: call_7hTYtlTj9NgWyw8AQGqETtV9
  Args:
    __arg1: 3 * 3 * 3
================================= Tool Message =================================
Name: do_math

[{'type': 'custom_tool_call_output', 'output': '27'}]
================================== Ai Message ==================================

[{'type': 'text', 'text': '27', 'annotations': [], 'id': 'msg_68b733f4bb008194937130796372bd0f0811f11b6f4bae47'}]

结构化输出

OpenAI 支持原生结构化输出功能,可保证其响应符合给定的 schema。 您可以在单次模型调用中使用此功能,也可以通过指定 LangChain Agent响应格式来使用。具体示例请参见下文。
使用 with_structured_output 方法生成结构化模型响应。指定 method="json_schema" 以启用 OpenAI 的原生结构化输出功能;否则,该方法默认使用函数调用。
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field

llm = ChatOpenAI(model="gpt-4.1")

class Movie(BaseModel):
    """A movie with details."""
    title: str = Field(..., description="The title of the movie")
    year: int = Field(..., description="The year the movie was released")
    director: str = Field(..., description="The director of the movie")
    rating: float = Field(..., description="The movie's rating out of 10")

structured_llm = llm.with_structured_output(Movie, method="json_schema")
response = structured_llm.invoke("Provide details about the movie Inception")
response
Movie(title='Inception', year=2010, director='Christopher Nolan', rating=8.8)
使用 ProviderStrategy 指定 response_format,在生成最终响应时启用 OpenAI 的结构化输出功能。
from langchain.agents import create_agent
from langchain.agents.structured_output import ProviderStrategy
from pydantic import BaseModel

class Weather(BaseModel):
    temperature: float
    condition: str

def weather_tool(location: str) -> str:
    """Get the weather at a location."""
    return "Sunny and 75 degrees F."

agent = create_agent(
    model="openai:gpt-4.1",
    tools=[weather_tool],
    response_format=ProviderStrategy(Weather),
)

result = agent.invoke({
    "messages": [{"role": "user", "content": "What's the weather in SF?"}]
})

result["structured_response"]
Weather(temperature=75.0, condition='Sunny')

结构化输出与工具调用

OpenAI 的结构化输出功能可以与工具调用同时使用,模型将生成工具调用或符合所需 schema 的响应。示例如下:
from langchain_openai import ChatOpenAI
from pydantic import BaseModel


def get_weather(location: str) -> None:
    """Get weather at a location."""
    return "It's sunny."


class OutputSchema(BaseModel):
    """Schema for response."""

    answer: str
    justification: str


llm = ChatOpenAI(model="gpt-4.1")

structured_llm = llm.bind_tools(
    [get_weather],
    response_format=OutputSchema,
    strict=True,
)

# 响应包含工具调用:
tool_call_response = structured_llm.invoke("What is the weather in SF?")

# structured_response.additional_kwargs["parsed"] 包含解析后的输出
structured_response = structured_llm.invoke(
    "What weighs more, a pound of feathers or a pound of gold?"
)

Responses API

需要 langchain-openai>=0.3.9
OpenAI 支持面向构建 Agent 应用的 Responses API。它包含一套内置工具,包括网络搜索和文件搜索。它还支持会话状态管理,让您无需显式传入历史消息即可延续对话线程,以及推理过程的输出。 当使用上述功能之一时,ChatOpenAI 会自动路由到 Responses API。您也可以在实例化 ChatOpenAI 时指定 use_responses_api=True

网络搜索

要触发网络搜索,请像传入其他工具一样将 {"type": "web_search_preview"} 传递给模型。
您也可以将内置工具作为调用参数传入:
llm.invoke("...", tools=[{"type": "web_search_preview"}])
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4.1-mini")

tool = {"type": "web_search_preview"}
llm_with_tools = llm.bind_tools([tool])

response = llm_with_tools.invoke("What was a positive news story from today?")
响应包含结构化内容块,其中包含响应文本和引用来源的 OpenAI 标注。输出消息还将包含工具调用的相关信息:
response.content_blocks
[{'type': 'server_tool_call',
  'name': 'web_search',
  'args': {'query': 'positive news stories today', 'type': 'search'},
  'id': 'ws_68cd6f8d72e4819591dab080f4b0c340080067ad5ea8144a'},
 {'type': 'server_tool_result',
  'tool_call_id': 'ws_68cd6f8d72e4819591dab080f4b0c340080067ad5ea8144a',
  'status': 'success'},
 {'type': 'text',
  'text': 'Here are some positive news stories from today...',
  'annotations': [{'end_index': 410,
    'start_index': 337,
    'title': 'Positive News | Real Stories. Real Positive Impact',
    'type': 'citation',
    'url': 'https://www.positivenews.press/?utm_source=openai'},
   {'end_index': 969,
    'start_index': 798,
    'title': "From Green Innovation to Community Triumphs: Uplifting US Stories Lighting Up September 2025 | That's Great News",
    'type': 'citation',
    'url': 'https://info.thatsgreatnews.com/from-green-innovation-to-community-triumphs-uplifting-us-stories-lighting-up-september-2025/?utm_source=openai'},
  'id': 'msg_68cd6f8e8d448195a807b89f483a1277080067ad5ea8144a'}]
您可以通过 response.text 以字符串形式获取响应的纯文本内容。例如,流式传输响应文本:
for token in llm_with_tools.stream("..."):
    print(token.text, end="|")
详情请参阅流式输出指南

图像生成

需要 langchain-openai>=0.3.19
要触发图像生成,请像传入其他工具一样将 {"type": "image_generation"} 传递给模型。
您也可以将内置工具作为调用参数传入:
llm.invoke("...", tools=[{"type": "image_generation"}])
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4.1-mini")

tool = {"type": "image_generation", "quality": "low"}

llm_with_tools = llm.bind_tools([tool])

ai_message = llm_with_tools.invoke(
    "Draw a picture of a cute fuzzy cat with an umbrella"
)
import base64

from IPython.display import Image

image = next(
    item for item in ai_message.content_blocks if item["type"] == "image"
)
Image(base64.b64decode(image["base64"]), width=200)

文件搜索

要触发文件搜索,请像传入其他工具一样将文件搜索工具传递给模型。您需要填充 OpenAI 管理的向量数据库,并在工具定义中包含向量数据库 ID。详情请参阅 OpenAI 文档
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4.1-mini",
    include=["file_search_call.results"],  # 可选:包含搜索结果
)

openai_vector_store_ids = [
    "vs_...",  # 您的 ID
]

tool = {
    "type": "file_search",
    "vector_store_ids": openai_vector_store_ids,
}
llm_with_tools = llm.bind_tools([tool])

response = llm_with_tools.invoke("What is deep research by OpenAI?")
print(response.text)
Deep Research by OpenAI is...
网络搜索类似,响应将包含带有引用的内容块:
[block["type"] for block in response.content_blocks]
['server_tool_call', 'server_tool_result', 'text']
text_block = next(block for block in response.content_blocks if block["type"] == "text")

text_block["annotations"][:2]
[{'type': 'citation',
  'title': 'deep_research_blog.pdf',
  'extras': {'file_id': 'file-3UzgX7jcC8Dt9ZAFzywg5k', 'index': 2712}},
 {'type': 'citation',
  'title': 'deep_research_blog.pdf',
  'extras': {'file_id': 'file-3UzgX7jcC8Dt9ZAFzywg5k', 'index': 2712}}]
响应还将包含内置工具调用的信息:
response.content_blocks[0]
{'type': 'server_tool_call',
 'name': 'file_search',
 'id': 'fs_68cd704c191c81959281b3b2ec6b139908f8f7fb31b1123c',
 'args': {'queries': ['deep research by OpenAI']}}

计算机使用

ChatOpenAI 支持 "computer-use-preview" 模型,这是一个专为内置计算机使用工具设计的特殊模型。要启用该功能,请像传入其他工具一样传入计算机使用工具 目前,计算机使用的工具输出位于消息 content 字段中。要回复计算机使用工具调用,需构造一个 ToolMessage,并在其 additional_kwargs 中设置 {"type": "computer_call_output"},消息内容为截图。以下演示一个简单示例。 首先,加载两张截图:
import base64


def load_png_as_base64(file_path):
    with open(file_path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
        return encoded_string.decode("utf-8")


screenshot_1_base64 = load_png_as_base64(
    "/path/to/screenshot_1.png"
)  # 例如某个应用程序的截图
screenshot_2_base64 = load_png_as_base64(
    "/path/to/screenshot_2.png"
)  # 例如桌面的截图
from langchain_openai import ChatOpenAI

# 初始化模型
llm = ChatOpenAI(model="computer-use-preview", truncation="auto")

# 绑定计算机使用工具
tool = {
    "type": "computer_use_preview",
    "display_width": 1024,
    "display_height": 768,
    "environment": "browser",
}
llm_with_tools = llm.bind_tools([tool])

# 构造输入消息
input_message = {
    "role": "user",
    "content": [
        {
            "type": "text",
            "text": (
                "Click the red X to close and reveal my Desktop. "
                "Proceed, no confirmation needed."
            ),
        },
        {
            "type": "input_image",
            "image_url": f"data:image/png;base64,{screenshot_1_base64}",
        },
    ],
}

# 调用模型
response = llm_with_tools.invoke(
    [input_message],
    reasoning={
        "generate_summary": "concise",
    },
)
响应将在其 content 中包含对计算机使用工具的调用:
response.content
[{'id': 'rs_685da051742c81a1bb35ce46a9f3f53406b50b8696b0f590',
  'summary': [{'text': "Clicking red 'X' to show desktop",
    'type': 'summary_text'}],
  'type': 'reasoning'},
 {'id': 'cu_685da054302481a1b2cc43b56e0b381706b50b8696b0f590',
  'action': {'button': 'left', 'type': 'click', 'x': 14, 'y': 38},
  'call_id': 'call_zmQerFBh4PbBE8mQoQHkfkwy',
  'pending_safety_checks': [],
  'status': 'completed',
  'type': 'computer_call'}]
接下来,构造具有以下属性的 ToolMessage
  1. tool_call_id 与计算机调用的 call_id 匹配。
  2. additional_kwargs 中包含 {"type": "computer_call_output"}
  3. 其内容为 image_urlinput_image 输出块(格式请参阅 OpenAI 文档)。
from langchain.messages import ToolMessage

tool_call_id = next(
    item["call_id"] for item in response.content if item["type"] == "computer_call"
)

tool_message = ToolMessage(
    content=[
        {
            "type": "input_image",
            "image_url": f"data:image/png;base64,{screenshot_2_base64}",
        }
    ],
    # content=f"data:image/png;base64,{screenshot_2_base64}",  # <-- 也可接受
    tool_call_id=tool_call_id,
    additional_kwargs={"type": "computer_call_output"},
)
现在可以使用消息历史再次调用模型:
messages = [
    input_message,
    response,
    tool_message,
]

response_2 = llm_with_tools.invoke(
    messages,
    reasoning={
        "generate_summary": "concise",
    },
)
response_2.text
'VS Code has been closed, and the desktop is now visible.'
也可以使用 previous_response_id 替代传回完整消息序列:
previous_response_id = response.response_metadata["id"]

response_2 = llm_with_tools.invoke(
    [tool_message],
    previous_response_id=previous_response_id,
    reasoning={
        "generate_summary": "concise",
    },
)
response_2.text
'The VS Code window is closed, and the desktop is now visible. Let me know if you need any further assistance.'

代码解释器

OpenAI 实现了代码解释器工具,支持在沙箱中生成和执行代码。
Example use
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4.1-mini",
    include=["code_interpreter_call.outputs"],  # 可选:包含输出
)

llm_with_tools = llm.bind_tools(
    [
        {
            "type": "code_interpreter",
            # 创建新容器
            "container": {"type": "auto"},
        }
    ]
)
response = llm_with_tools.invoke(
    "Write and run code to answer the question: what is 3^3?"
)
注意上述命令创建了一个新容器,也可以指定现有容器 ID:
code_interpreter_calls = [
    item for item in response.content if item["type"] == "code_interpreter_call"
]
assert len(code_interpreter_calls) == 1
container_id = code_interpreter_calls[0]["extras"]["container_id"]

llm_with_tools = llm.bind_tools(
    [
        {
            "type": "code_interpreter",
            # 使用现有容器
            "container": container_id,
        }
    ]
)

远程 MCP

OpenAI 实现了远程 MCP 工具,允许模型调用 MCP 服务器。
Example use
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4.1-mini")

llm_with_tools = llm.bind_tools(
    [
        {
            "type": "mcp",
            "server_label": "deepwiki",
            "server_url": "https://mcp.deepwiki.com/mcp",
            "require_approval": "never",
        }
    ]
)
response = llm_with_tools.invoke(
    "What transport protocols does the 2025-03-26 version of the MCP "
    "spec (modelcontextprotocol/modelcontextprotocol) support?"
)
OpenAI 有时会在与远程 MCP 服务器共享数据之前请求审批。在上述命令中,我们指示模型永不需要审批。我们也可以配置模型始终请求审批,或对特定工具始终请求审批:
llm_with_tools = llm.bind_tools(
    [
        {
            "type": "mcp",
            "server_label": "deepwiki",
            "server_url": "https://mcp.deepwiki.com/mcp",
            "require_approval": {
                "always": {
                    "tool_names": ["read_wiki_structure"]
                }
            }
        }
    ]
)
response = llm_with_tools.invoke(
    "What transport protocols does the 2025-03-26 version of the MCP "
    "spec (modelcontextprotocol/modelcontextprotocol) support?"
)
响应中可能包含类型为 "mcp_approval_request" 的块。要提交审批请求的批准,请将其结构化为输入消息中的内容块:
approval_message = {
    "role": "user",
    "content": [
        {
            "type": "mcp_approval_response",
            "approve": True,
            "approval_request_id": block["id"],
        }
        for block in response.content
        if block["type"] == "mcp_approval_request"
    ]
}

next_response = llm_with_tools.invoke(
    [approval_message],
    # 继续现有对话线程
    previous_response_id=response.response_metadata["id"]
)

管理会话状态

Responses API 支持会话状态管理。

手动管理状态

您可以像使用其他聊天模型一样,手动管理状态或使用 LangGraph
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4.1-mini", use_responses_api=True)

first_query = "Hi, I'm Bob."
messages = [{"role": "user", "content": first_query}]

response = llm.invoke(messages)
print(response.text)
Hi Bob! Nice to meet you. How can I assist you today?
second_query = "What is my name?"

messages.extend(
    [
        response,
        {"role": "user", "content": second_query},
    ]
)
second_response = llm.invoke(messages)
print(second_response.text)
You mentioned that your name is Bob. How can I assist you further, Bob?
您可以使用 LangGraph 在各种后端(包括内存和 Postgres)中自动管理对话线程。请参阅本教程快速入门。

传入 previous_response_id

使用 Responses API 时,LangChain 消息的元数据中将包含 "id" 字段。将该 ID 传入后续调用将延续对话。注意从计费角度来看,这与手动传入消息等效
second_response = llm.invoke(
    "What is my name?",
    previous_response_id=response.id,
)
print(second_response.text)
Your name is Bob. How can I help you today, Bob?
ChatOpenAI 还可以使用消息序列中的最后一条响应自动指定 previous_response_id
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4.1-mini",
    use_previous_response_id=True,
)
如果设置 use_previous_response_id=True,请求负载中最新响应之前的输入消息将被丢弃,previous_response_id 将使用最新响应的 ID 设置。 也就是说,
llm.invoke(
    [
        HumanMessage("Hello"),
        AIMessage("Hi there!", id="resp_123"),
        HumanMessage("How are you?"),
    ]
)
…等效于:
llm.invoke([HumanMessage("How are you?")], previous_response_id="resp_123")

上下文管理

Responses API 支持自动服务端上下文压缩。当对话达到 token 阈值时,自动压缩对话大小,支持长时间运行的交互:
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model="gpt-5.2",
    context_management=[
        {"type": "compaction", "compact_threshold": 100_000}
    ],
)
启用后,AIMessage 响应的内容中可能包含 "type": "compaction" 的块。这些块应保留在对话历史中,可以按常规方式追加到消息序列中。最近一次 compaction 条目之前的消息可以保留,也可以丢弃以提升延迟。

推理输出

某些 OpenAI 模型会生成单独的文本内容来说明其推理过程。详情请参阅 OpenAI 的推理文档 OpenAI 可以返回模型推理的摘要(尽管不公开原始推理 token)。要配置 ChatOpenAI 返回此摘要,请指定 reasoning 参数。如果设置了该参数,ChatOpenAI 将自动路由到 Responses API。
from langchain_openai import ChatOpenAI

reasoning = {
    "effort": "medium",  # 'low'、'medium' 或 'high'
    "summary": "auto",  # 'detailed'、'auto' 或 None
}

llm = ChatOpenAI(model="gpt-5-nano", reasoning=reasoning)
response = llm.invoke("What is 3^3?")

# 输出
response.text
'3³ = 3 × 3 × 3 = 27.'
# 推理过程
for block in response.content_blocks:
    if block["type"] == "reasoning":
        print(block["reasoning"])
**Calculating the power of three**

The user is asking about 3 raised to the power of 3. That's a pretty simple calculation! I know that 3^3 equals 27, so I can say, "3 to the power of 3 equals 27." I might also include a quick explanation that it's 3 multiplied by itself three times: 3 × 3 × 3 = 27. So, the answer is definitely 27.
故障排除:推理模型返回空响应如果您从推理模型(如 gpt-5-nano)获得空响应,这可能是由于 token 限制过于严格。该模型使用 token 进行内部推理,可能没有剩余 token 用于最终输出。请确保将 max_tokens 设置为 None 或增加 token 限制,以为推理和输出生成提供足够的 token。

微调

您可以通过传入相应的 modelName 参数来调用微调后的 OpenAI 模型。 一般格式为 ft:{OPENAI_MODEL_NAME}:{ORG_NAME}::{MODEL_ID},例如:
fine_tuned_model = ChatOpenAI(
    temperature=0, model_name="ft:gpt-3.5-turbo-0613:langchain::7qTVM5AR"
)

fine_tuned_model.invoke(messages)
AIMessage(content="J'adore la programmation.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 8, 'prompt_tokens': 31, 'total_tokens': 39}, 'model_name': 'ft:gpt-3.5-turbo-0613:langchain::7qTVM5AR', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-0f39b30e-c56e-4f3b-af99-5c948c984146-0', usage_metadata={'input_tokens': 31, 'output_tokens': 8, 'total_tokens': 39})

多模态输入(图像、PDF、音频)

OpenAI 的部分模型支持多模态输入,您可以向这些模型传入图像、PDF 或音频。有关如何在 LangChain 中实现这一点,请参阅多模态输入文档。 您可以在 OpenAI 文档中查看支持不同模态的模型列表。 对于所有模态,LangChain 同时支持其跨提供商标准格式和 OpenAI 的原生内容块格式。 要将多模态数据传入 ChatOpenAI,请创建包含数据的内容块并将其加入消息,例如:
message = {
    "role": "user",
    "content": [
        {
            "type": "text",
            # 根据需要更新提示
            "text": "Describe the (image / PDF / audio...)",
        },
        content_block,
    ],
}
内容块示例请参见下文。
具体示例请参阅此处的操作指南
URLs
# LangChain 格式
content_block = {
    "type": "image",
    "url": url_string,
}

# OpenAI 聊天补全格式
content_block = {
    "type": "image_url",
    "image_url": {"url": url_string},
}
In-line base64 data
# LangChain 格式
content_block = {
    "type": "image",
    "base64": base64_string,
    "mime_type": "image/jpeg",
}

# OpenAI 聊天补全格式
content_block = {
    "type": "image_url",
    "image_url": {
        "url": f"data:image/jpeg;base64,{base64_string}",
    },
}
注意:OpenAI 要求为 PDF 输入指定文件名。使用 LangChain 格式时,请包含 filename 键。更多信息请参阅此处具体示例请参阅此处的操作指南
In-line base64 data
# LangChain 格式
content_block = {
    "type": "file",
    "base64": base64_string,
    "mime_type": "application/pdf",
    "filename": "my-file.pdf",
}

# OpenAI 聊天补全格式
content_block = {
    "type": "file",
    "file": {
        "filename": "my-file.pdf",
        "file_data": f"data:application/pdf;base64,{base64_string}",
    }
}
请参阅支持的模型,例如 "gpt-4o-audio-preview"具体示例请参阅此处的操作指南
In-line base64 data
# LangChain 格式
content_block = {
    "type": "audio",
    "mime_type": "audio/wav",  # 或其他合适的 MIME 类型
    "base64": base64_string,
}

# OpenAI 聊天补全格式
content_block = {
    "type": "input_audio",
    "input_audio": {"data": base64_string, "format": "wav"},
}

预测输出

需要 langchain-openai>=0.2.6
某些 OpenAI 模型(如 gpt-4ogpt-4o-mini 系列)支持预测输出,允许您提前传入 LLM 预期输出的已知部分,以降低延迟。这在只有少部分模型输出会发生变化的场景中非常有用,例如文本或代码编辑。 以下是一个示例:
code = """
/// <summary>
/// Represents a user with a first name, last name, and username.
/// </summary>
public class User
{
    /// <summary>
    /// Gets or sets the user's first name.
    /// </summary>
    public string FirstName { get; set; }

    /// <summary>
    /// Gets or sets the user's last name.
    /// </summary>
    public string LastName { get; set; }

    /// <summary>
    /// Gets or sets the user's username.
    /// </summary>
    public string Username { get; set; }
}
"""

llm = ChatOpenAI(model="gpt-4.1")
query = (
    "Replace the Username property with an Email property. "
    "Respond only with code, and with no markdown formatting."
)
response = llm.invoke(
    [{"role": "user", "content": query}, {"role": "user", "content": code}],
    prediction={"type": "content", "content": code},
)
print(response.content)
print(response.response_metadata)
/// <summary>
/// Represents a user with a first name, last name, and email.
/// </summary>
public class User
{
    /// <summary>
    /// Gets or sets the user's first name.
    /// </summary>
    public string FirstName { get; set; }

    /// <summary>
    /// Gets or sets the user's last name.
    /// </summary>
    public string LastName { get; set; }

    /// <summary>
    /// Gets or sets the user's email.
    /// </summary>
    public string Email { get; set; }
}
{'token_usage': {'completion_tokens': 226, 'prompt_tokens': 166, 'total_tokens': 392, 'completion_tokens_details': {'accepted_prediction_tokens': 49, 'audio_tokens': None, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 107}, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_45cf54deae', 'finish_reason': 'stop', 'logprobs': None}
预测输出会作为额外 token 计费,可能会增加您的用量和费用,以换取更低的延迟。

音频生成(预览版)

需要 langchain-openai>=0.2.3
OpenAI 提供了新的音频生成功能,允许您通过 gpt-4o-audio-preview 模型使用音频输入和输出。
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4o-audio-preview",
    temperature=0,
    model_kwargs={
        "modalities": ["text", "audio"],
        "audio": {"voice": "alloy", "format": "wav"},
    },
)

output_message = llm.invoke(
    [
        ("human", "Are you made by OpenAI? Just answer yes or no"),
    ]
)
output_message.additional_kwargs['audio'] 将包含如下字典:
{
    'data': '<audio data b64-encoded',
    'expires_at': 1729268602,
    'id': 'audio_67127d6a44348190af62c1530ef0955a',
    'transcript': 'Yes.'
}
…格式将是在 model_kwargs['audio']['format'] 中传入的格式。 我们还可以在 openai expires_at 到期之前,将包含音频数据的消息作为消息历史的一部分传回模型。
输出音频存储在 AIMessage.additional_kwargsaudio 键下,但输入内容块在 HumanMessage.content 列表中的类型和键为 input_audio更多信息请参阅 OpenAI 的音频文档
history = [
    ("human", "Are you made by OpenAI? Just answer yes or no"),
    output_message,
    ("human", "And what is your name? Just give your name."),
]
second_output_message = llm.invoke(history)

提示缓存

OpenAI 的提示缓存功能会自动缓存超过 1024 个 token 的提示,以降低成本并提升响应时间。此功能对所有近期模型(gpt-4o 及更新版本)均已启用。

手动缓存

您可以使用 prompt_cache_key 参数来影响 OpenAI 的缓存并优化缓存命中率:
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4.1")

# 对重复提示使用缓存键
messages = [
    {"role": "system", "content": "You are a helpful assistant that translates English to French."},
    {"role": "user", "content": "I love programming."},
]

response = llm.invoke(
    messages,
    prompt_cache_key="translation-assistant-v1"
)

# 检查缓存使用情况
cache_read_tokens = response.usage_metadata.input_token_details.cache_read
print(f"Cached tokens used: {cache_read_tokens}")
缓存命中要求提示前缀完全匹配

缓存键策略

您可以根据应用程序的需求使用不同的缓存键策略:
# 对一致的提示模板使用静态缓存键
customer_response = llm.invoke(
    messages,
    prompt_cache_key="customer-support-v1"
)

support_response = llm.invoke(
    messages,
    prompt_cache_key="internal-support-v1"
)

# 基于上下文的动态缓存键
user_type = "premium"
cache_key = f"assistant-{user_type}-v1"
response = llm.invoke(messages, prompt_cache_key=cache_key)

模型级缓存

您也可以通过 model_kwargs 在模型层面设置默认缓存键:
llm = ChatOpenAI(
    model="gpt-4.1-mini",
    model_kwargs={"prompt_cache_key": "default-cache-v1"}
)

# 使用默认缓存键
response1 = llm.invoke(messages)

# 使用特定缓存键覆盖
response2 = llm.invoke(messages, prompt_cache_key="override-cache-v1")

Flex 处理

OpenAI 提供多种服务层级。“flex” 层级以可能更长的响应时间和资源不保证可用为代价,提供更低的定价。此方式最适合非关键任务,包括模型测试、数据增强或可异步运行的任务。 使用时,请在初始化模型时设置 service_tier="flex"
llm = ChatOpenAI(model="o4-mini", service_tier="flex")
注意,这是一个仅对部分模型可用的测试功能。详情请参阅 OpenAI 文档

API 参考

有关所有功能和配置选项的详细文档,请参阅 ChatOpenAI API 参考。