motleycrew.agents.langchain

Modules

langchain

legacy_react

tool_calling_react

tool_calling_react_prompts

class motleycrew.agents.langchain.LangchainMotleyAgent(description: str | None = None, name: str | None = None, prompt: str | ChatPromptTemplate | None = None, agent_factory: MotleyAgentFactory[AgentExecutor] | None = None, tools: Sequence[MotleySupportedTool] | None = None, force_output_handler: bool = False, chat_history: bool | GetSessionHistoryCallable = True, input_as_messages: bool = False, runnable_config: RunnableConfig | None = None, verbose: bool = False)

Bases: MotleyAgentParent, LangchainOutputHandlingAgentMixin

MotleyCrew wrapper for Langchain agents.

__init__(description: str | None = None, name: str | None = None, prompt: str | ChatPromptTemplate | None = None, agent_factory: MotleyAgentFactory[AgentExecutor] | None = None, tools: Sequence[MotleySupportedTool] | None = None, force_output_handler: bool = False, chat_history: bool | GetSessionHistoryCallable = True, input_as_messages: bool = False, runnable_config: RunnableConfig | None = None, verbose: bool = False)
Parameters:
  • description

    Description of the agent.

    Unlike the prompt prefix, it is not included in the prompt. The description is only used for describing the agent’s purpose when giving it as a tool to other agents.

  • name

    Name of the agent. The name is used for identifying the agent when it is given as a tool to other agents, as well as for logging purposes.

    It is not included in the agent’s prompt.

  • prompt

    Prompt to the agent.

    If a string, it will be used as a prompt. If a string containing f-string-style placeholders, it will be used as a prompt template. If a ChatPromptTemplate, it will be used as a prompt template.

  • agent_factory

    Factory function to create the agent. The factory function should accept a dictionary of tools and return an AgentExecutor instance.

    See motleycrew.common.types.MotleyAgentFactory for more details.

    Alternatively, you can use the from_agent() method to wrap an existing AgentExecutor.

  • tools – Tools to add to the agent.

  • force_output_handler – Whether to force the agent to return through an output handler. If True, at least one tool must have return_direct set to True.

  • chat_history

    Whether to use chat history or not. If True, uses InMemoryChatMessageHistory. If a callable is passed, it is used to get the chat history by session_id.

    See langchain_core.runnables.history.RunnableWithMessageHistory for more details.

  • input_as_messages – Whether the agent expects a list of messages as input instead of a single string.

  • runnable_config – Default Langchain config to use when invoking the agent. It can be used to add callbacks, metadata, etc.

  • verbose – Whether to log verbose output.

materialize()

Materialize the agent and wrap it in RunnableWithMessageHistory if needed.

invoke(input: str | dict, config: RunnableConfig | None = None, **kwargs: Any) Any

Transform a single input into an output.

Parameters:
  • input – The input to the Runnable.

  • config – A config to use when invoking the Runnable. The config supports standard keys like ‘tags’, ‘metadata’ for tracing purposes, ‘max_concurrency’ for controlling how much work to do in parallel, and other keys. Please refer to the RunnableConfig for more details.

Returns:

The output of the Runnable.

async ainvoke(input: dict, config: RunnableConfig | None = None, **kwargs: Any) Any

Default implementation of ainvoke, calls invoke from a thread.

The default implementation allows usage of async code even if the Runnable did not implement a native async version of invoke.

Subclasses should override this method if they can run asynchronously.

static from_agent(agent: AgentExecutor, description: str | None = None, prompt: str | ChatPromptTemplate | None = None, tools: Sequence[MotleySupportedTool] | None = None, runnable_config: RunnableConfig | None = None, verbose: bool = False) LangchainMotleyAgent

Create a LangchainMotleyAgent from a langchain.agents.AgentExecutor instance.

Using this method, you can wrap an existing AgentExecutor without providing a factory function.

Parameters:
  • agent – AgentExecutor instance to wrap.

  • prompt

    Prompt for the agent.

    If a string, it will be used as a prompt. If a string containing f-string-style placeholders, it will be used as a prompt template. If a ChatPromptTemplate, it will be used as a prompt template.

  • description

    Description of the agent.

    Unlike the prompt prefix, it is not included in the prompt. The description is only used for describing the agent’s purpose when giving it as a tool to other agents.

  • tools – Tools to add to the agent.

  • runnable_config – Default Langchain config to use when invoking the agent. It can be used to add callbacks, metadata, etc.

  • verbose – Whether to log verbose output.

class motleycrew.agents.langchain.LegacyReActMotleyAgent(tools: Sequence[MotleySupportedTool], description: str | None = None, name: str | None = None, prompt: str | None = None, chat_history: bool | GetSessionHistoryCallable = True, force_output_handler: bool = False, internal_prompt: str | None = None, handle_parsing_errors: bool = True, handle_tool_errors: bool = True, llm: BaseLanguageModel | None = None, runnable_config: RunnableConfig | None = None, verbose: bool = False)

Bases: LangchainMotleyAgent

Basic ReAct agent compatible with older models without dedicated tool calling support.

It’s probably better to use the more advanced motleycrew.agents.langchain.tool_calling_react.ReActToolCallingAgent with newer models.

__init__(tools: Sequence[MotleySupportedTool], description: str | None = None, name: str | None = None, prompt: str | None = None, chat_history: bool | GetSessionHistoryCallable = True, force_output_handler: bool = False, internal_prompt: str | None = None, handle_parsing_errors: bool = True, handle_tool_errors: bool = True, llm: BaseLanguageModel | None = None, runnable_config: RunnableConfig | None = None, verbose: bool = False)
Parameters:
  • tools – Tools to add to the agent.

  • description – Description of the agent.

  • name – Name of the agent.

  • prompt – Prompt for the agent. If a string, it will be used as a prompt. If a string containing f-string-style placeholders, it will be used as a prompt template.

  • chat_history – Whether to use chat history or not.

  • force_output_handler – Whether to force the agent to return through an output handler.

  • internal_prompt – Internal prompt to use with the agent.

  • handle_parsing_errors – Whether to handle parsing errors.

  • handle_tool_errors – Whether to handle tool errors.

  • llm – Language model to use.

  • runnable_config – Default Langchain config to use when invoking the agent. It can be used to add callbacks, metadata, etc.

  • verbose – Whether to log verbose output.

class motleycrew.agents.langchain.ReActToolCallingMotleyAgent(tools: Sequence[MotleySupportedTool], description: str | None = None, name: str | None = None, prompt: str | ChatPromptTemplate | None = None, chat_history: bool | GetSessionHistoryCallable = True, force_output_handler: bool = False, handle_parsing_errors: bool = False, llm: BaseChatModel | None = None, max_iterations: int | None = 15, internal_prompt: ChatPromptTemplate | None = None, intermediate_steps_processor: Callable | None = None, runnable_config: RunnableConfig | None = None, verbose: bool = False, prompt_prefix: str | None = None)

Bases: LangchainMotleyAgent

Universal ReAct-style agent that supports tool calling.

This agent only works with newer models that support tool calling. If you are using an older model, you should use motleycrew.agents.langchain.LegacyReActMotleyAgent instead.

__init__(tools: Sequence[MotleySupportedTool], description: str | None = None, name: str | None = None, prompt: str | ChatPromptTemplate | None = None, chat_history: bool | GetSessionHistoryCallable = True, force_output_handler: bool = False, handle_parsing_errors: bool = False, llm: BaseChatModel | None = None, max_iterations: int | None = 15, internal_prompt: ChatPromptTemplate | None = None, intermediate_steps_processor: Callable | None = None, runnable_config: RunnableConfig | None = None, verbose: bool = False, prompt_prefix: str | None = None)
Parameters:
  • tools – Tools to add to the agent.

  • description – Description of the agent.

  • name – Name of the agent.

  • prompt

    Prompt for the agent.

    If a string, it will be used as a prompt. If a string containing f-string-style placeholders, it will be used as a prompt template. If a ChatPromptTemplate, it will be used as a prompt template.

  • chat_history – Whether to use chat history or not. If True, uses InMemoryChatMessageHistory. If a callable is passed, it is used to get the chat history by session_id. See langchain_core.runnables.history.RunnableWithMessageHistory for more details.

  • force_output_handler – Whether to force the agent to return through an output handler. If True, at least one tool must have return_direct set to True.

  • handle_parsing_errors – Whether to handle parsing errors.

  • handle_tool_errors – Whether to handle tool errors. If True, handle_tool_error and handle_validation_error in all tools are set to True.

  • llm – Language model to use.

  • max_iterations – The maximum number of agent iterations.

  • internal_prompt – The internal ReAct prompt to use. See Internal prompt section below for more on the expected input variables.

  • intermediate_steps_processor – Function that modifies the intermediate steps array in some way before each agent iteration.

  • runnable_config – Default Langchain config to use when invoking the agent. It can be used to add callbacks, metadata, etc.

  • verbose – Whether to log verbose output.

  • prompt_prefix – Deprecated. Please use the prompt argument instead.

Internal prompt:

Not to be confused with the prompt argument, which is user-facing and is the recommended way to explain the task to the agent.

The internal (system) prompt contains the explanations for the low-level ReAct agent behavior (tool calling, reasoning, etc). Only modify this if you know what you are doing.

The internal prompt must have agent_scratchpad, chat_history, and additional_notes ``MessagesPlaceholder``s.

The default prompt slightly varies depending on the language model used. See motleycrew.agents.langchain.tool_calling_react_prompts for more details.