LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • MCP Adapters
    • Overview
    • Agents
    • Callbacks
    • Chains
    • Chat models
    • Embeddings
    • Evaluation
    • Globals
    • Hub
    • Memory
    • Output parsers
    • Retrievers
    • Runnables
    • LangSmith
    • Storage
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    MCP Adapters
    OverviewAgentsCallbacksChainsChat modelsEmbeddingsEvaluationGlobalsHubMemoryOutput parsersRetrieversRunnablesLangSmithStorage
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-classicagentsopenai_assistantbaseOpenAIAssistantRunnable
    Class●Since v1.0

    OpenAIAssistantRunnable

    Copy
    OpenAIAssistantRunnable()

    Bases

    RunnableSerializable[dict, OutputType]

    Attributes

    Methods

    Inherited fromRunnableSerializable(langchain_core)

    Attributes

    AnameAmodel_config

    Methods

    Mto_jsonMconfigurable_fields
    View source on GitHub
    M
    configurable_alternatives

    Inherited fromSerializable(langchain_core)

    Attributes

    Alc_secretsAlc_attributesAmodel_config

    Methods

    Mis_lc_serializableMget_lc_namespaceMlc_idMto_jsonMto_json_not_implemented

    Inherited fromRunnable(langchain_core)

    Attributes

    AnameAInputTypeAOutputTypeAinput_schemaAoutput_schemaAconfig_specs

    Methods

    Mget_nameMget_input_schemaMget_input_jsonschemaMget_output_schemaMget_output_jsonschemaM
    attribute
    client: Any

    OpenAI or AzureOpenAI client.

    attribute
    async_client: Any

    OpenAI or AzureOpenAI async client.

    attribute
    assistant_id: str

    OpenAI assistant id.

    attribute
    check_every_ms: float

    Frequency with which to check run progress in ms.

    attribute
    as_agent: bool

    Use as a LangChain agent, compatible with the AgentExecutor.

    method
    create_assistant

    Create an OpenAI Assistant and instantiate the Runnable.

    method
    invoke

    Invoke assistant.

    method
    acreate_assistant

    Async create an AsyncOpenAI Assistant and instantiate the Runnable.

    method
    ainvoke

    Async invoke assistant.

    Run an OpenAI Assistant.

    Example using OpenAI tools:

    from langchain_experimental.openai_assistant import OpenAIAssistantRunnable
    
    interpreter_assistant = OpenAIAssistantRunnable.create_assistant(
        name="langchain assistant",
        instructions="You are a personal math tutor. "
        "Write and run code to answer math questions.",
        tools=[{"type": "code_interpreter"}],
        model="gpt-4-1106-preview",
    )
    output = interpreter_assistant.invoke(
        {"content": "What's 10 - 4 raised to the 2.7"}
    )

    Example using custom tools and AgentExecutor:

    from langchain_experimental.openai_assistant import OpenAIAssistantRunnable
    from langchain_classic.agents import AgentExecutor
    from langchain_classic.tools import E2BDataAnalysisTool
    
    tools = [E2BDataAnalysisTool(api_key="...")]
    agent = OpenAIAssistantRunnable.create_assistant(
        name="langchain assistant e2b tool",
        instructions="You are a personal math tutor. "
        "Write and run code to answer math questions.",
        tools=tools,
        model="gpt-4-1106-preview",
        as_agent=True,
    )
    
    agent_executor = AgentExecutor(agent=agent, tools=tools)
    agent_executor.invoke({"content": "What's 10 - 4 raised to the 2.7"})

    Example using custom tools and custom execution:

    from langchain_experimental.openai_assistant import OpenAIAssistantRunnable
    from langchain_classic.agents import AgentExecutor
    from langchain_core.agents import AgentFinish
    from langchain_classic.tools import E2BDataAnalysisTool
    
    tools = [E2BDataAnalysisTool(api_key="...")]
    agent = OpenAIAssistantRunnable.create_assistant(
        name="langchain assistant e2b tool",
        instructions="You are a personal math tutor. "
        "Write and run code to answer math questions.",
        tools=tools,
        model="gpt-4-1106-preview",
        as_agent=True,
    )
    
    def execute_agent(agent, tools, input):
        tool_map = {tool.name: tool for tool in tools}
        response = agent.invoke(input)
        while not isinstance(response, AgentFinish):
            tool_outputs = []
            for action in response:
                tool_output = tool_map[action.tool].invoke(action.tool_input)
                tool_outputs.append(
                    {
                        "output": tool_output,
                        "tool_call_id": action.tool_call_id,
                    }
                )
            response = agent.invoke(
                {
                    "tool_outputs": tool_outputs,
                    "run_id": action.run_id,
                    "thread_id": action.thread_id,
                }
            )
    
        return response
    
    response = execute_agent(
        agent, tools, {"content": "What's 10 - 4 raised to the 2.7"}
    )
    next_response = execute_agent(
        agent,
        tools,
        {"content": "now add 17.241", "thread_id": response.thread_id},
    )
    config_schema
    Mget_config_jsonschema
    Mget_graph
    Mget_prompts
    Mpipe
    Mpick
    Massign
    Mbatch
    Mbatch_as_completed
    Mabatch
    Mabatch_as_completed
    Mstream
    Mastream
    Mastream_log
    Mastream_events
    Mtransform
    Matransform
    Mbind
    Mwith_config
    Mwith_listeners
    Mwith_alisteners
    Mwith_types
    Mwith_retry
    Mmap
    Mwith_fallbacks
    Mas_tool