Skip to content

LLMToolEmulator Class Redundant Condition, and ImportError with Default State #34274

@ahmed33033

Description

@ahmed33033

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-cli
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-perplexity
  • langchain-prompty
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Example Code (Python)

from langchain.agents import create_agent
from langchain.tools import tool
from langchain.agents.middleware import LLMToolEmulator
from dotenv import load_dotenv
from pydantic import BaseModel, Field

load_dotenv()

class GetCarSchema(BaseModel):
    car: str = Field(strict=True, description="The name of the car.")

@tool(args_schema=GetCarSchema)
def get_car_price(car: str) -> str:
    """
    Get the price of a car by simply using the name.
    """
    return f"${len(car)*1000 + len(car)*50}" 
    

def main():
    agent = create_agent(model="gpt-5-mini-2025-08-07",
                 tools=[get_car_price],
                 middleware=[LLMToolEmulator()])
    agent.invoke({"messages": [{"role": "user", "content": "What's the price of a Camry?"}]}, print_mode="values")

if __name__ == "__main__":
    main()

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "/home/ahmed/Desktop/programming_files/langchain-tester/main.py", line 27, in <module>
    main()
  File "/home/ahmed/Desktop/programming_files/langchain-tester/main.py", line 23, in main
    middleware=[LLMToolEmulator()])
                ^^^^^^^^^^^^^^^^^
  File "/home/ahmed/Desktop/programming_files/langchain-tester/.venv/lib/python3.12/site-packages/langchain/agents/middleware/tool_emulator.py", line 103, in __init__
    self.model = init_chat_model("anthropic:claude-sonnet-4-5-20250929", temperature=1)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ahmed/Desktop/programming_files/langchain-tester/.venv/lib/python3.12/site-packages/langchain/chat_models/base.py", line 316, in init_chat_model
    return _init_chat_model_helper(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ahmed/Desktop/programming_files/langchain-tester/.venv/lib/python3.12/site-packages/langchain/chat_models/base.py", line 345, in _init_chat_model_helper
    _check_pkg("langchain_anthropic")
  File "/home/ahmed/Desktop/programming_files/langchain-tester/.venv/lib/python3.12/site-packages/langchain/chat_models/base.py", line 533, in _check_pkg
    raise ImportError(msg)
ImportError: Unable to import langchain_anthropic. Please install with `pip install -U langchain-anthropic`

Description

The issues can be traced to the LLMToolEmulator Class.. For convenience, the code in question is pasted below:

 super().__init__()

        # Extract tool names from tools
        # None means emulate all tools
        self.emulate_all = tools is None
        self.tools_to_emulate: set[str] = set()

        if not self.emulate_all and tools is not None:
            for tool in tools:
                if isinstance(tool, str):
                    self.tools_to_emulate.add(tool)
                else:
                    # Assume BaseTool with .name attribute
                    self.tools_to_emulate.add(tool.name)

        # Initialize emulator model
        if model is None:
            self.model = init_chat_model("anthropic:claude-sonnet-4-5-20250929", temperature=1)
        elif isinstance(model, BaseChatModel):
            self.model = model
        else:
            self.model = init_chat_model(model, temperature=1)

Issue 1: Redundant Condition in LLMToolEmulator Class

In the __init__ method of the LLMToolEmulator Class, self.emulate_all is set to tools is None. Two lines later, the if statement evaluates: if not self.emulate_all and tools is not None:. In this case, not self.emulate_all == tools is not None, so it's evaluating the same condition twice, which is redundant.

Issue 2: ImportError with Default State

The LLMToolEmulator class uses anthropic:claude-sonnet-4-5-20250929 as a default state. This is a bug for multiple reasons:

  1. The first example shown in LLMToolEmulator Class' documentation does not work if the langchain-anthropic extension is not installed, and the Anthropic API key is not a part of the environment variables.
  2. Without the langchain-anthropic extension, an ImportError is shown to the developer, which is not helpful.
  3. Most obvious, the LLMToolEmulator should not depend on an external extension to work.

The easy fix to this particular error is to remove the default state, and to make the keyword argument model required, as part of the __init__ method.

System Info

System Information

OS: Linux
OS Version: #17-Ubuntu SMP PREEMPT_DYNAMIC Mon Nov 24 08:52:02 UTC 2025
Python Version: 3.12.3 (main, Nov 6 2025, 13:44:16) [GCC 13.3.0]

Package Information

langchain_core: 1.1.3
langchain: 1.1.3
langsmith: 0.4.57
langchain_openai: 1.1.1
langgraph_sdk: 0.2.15

Optional packages not installed

langserve

Other Dependencies

httpx: 0.28.1
jsonpatch: 1.33
langgraph: 1.0.4
openai: 2.9.0
orjson: 3.11.5
packaging: 25.0
pydantic: 2.12.5
pyyaml: 6.0.3
requests: 2.32.5
requests-toolbelt: 1.0.0
tenacity: 9.1.2
tiktoken: 0.12.0
typing-extensions: 4.15.0
uuid-utils: 0.12.0
zstandard: 0.25.0

Metadata

Metadata

Assignees

Labels

bugRelated to a bug, vulnerability, unexpected error with an existing featurelangchain`langchain` package issues & PRsopenai`langchain-openai` package issues & PRs

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions