LLMMathChain Class — langchain Architecture
Architecture documentation for the LLMMathChain class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 54db45e3_8f5a_e755_e4fb_23f335cffa98["LLMMathChain"] 097a4781_5519_0b5d_6244_98c64eadc0d6["Chain"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|extends| 097a4781_5519_0b5d_6244_98c64eadc0d6 0851c588_52de_6693_758a_4d949d667f63["base.py"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|defined in| 0851c588_52de_6693_758a_4d949d667f63 066c6306_0b98_eeac_29fc_e587271af30a["_raise_deprecation()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| 066c6306_0b98_eeac_29fc_e587271af30a 1d7c53f9_9b81_0cdb_79ec_174c53e06d20["input_keys()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| 1d7c53f9_9b81_0cdb_79ec_174c53e06d20 05477227_00e7_14f2_066a_d0f1c05bd9a8["output_keys()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| 05477227_00e7_14f2_066a_d0f1c05bd9a8 26fc76f2_e675_bea5_1a32_d9d29918e38f["_evaluate_expression()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| 26fc76f2_e675_bea5_1a32_d9d29918e38f 58d0dc9a_f0f4_cf10_3a10_96782b144ce0["_process_llm_result()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| 58d0dc9a_f0f4_cf10_3a10_96782b144ce0 bce039c8_3a25_83da_5cea_51b3a420d375["_aprocess_llm_result()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| bce039c8_3a25_83da_5cea_51b3a420d375 9d0652c9_5cca_c2f8_b9e7_3ca7791915bc["_call()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| 9d0652c9_5cca_c2f8_b9e7_3ca7791915bc 23316940_b9c3_0c81_ee2c_e6fe045917f5["_acall()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| 23316940_b9c3_0c81_ee2c_e6fe045917f5 53e2e6a8_9ba8_358f_dd83_9a084ca9366c["_chain_type()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| 53e2e6a8_9ba8_358f_dd83_9a084ca9366c 037ab4ca_903d_9719_9377_0e54151da77e["from_llm()"] 54db45e3_8f5a_e755_e4fb_23f335cffa98 -->|method| 037ab4ca_903d_9719_9377_0e54151da77e
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/llm_math/base.py lines 33–315
class LLMMathChain(Chain):
"""Chain that interprets a prompt and executes python code to do math.
!!! note
This class is deprecated. See below for a replacement implementation using
LangGraph. The benefits of this implementation are:
- Uses LLM tool calling features;
- Support for both token-by-token and step-by-step streaming;
- Support for checkpointing and memory of chat history;
- Easier to modify or extend
(e.g., with additional tools, structured responses, etc.)
Install LangGraph with:
```bash
pip install -U langgraph
```
```python
import math
from typing import Annotated, Sequence
from langchain_core.messages import BaseMessage
from langchain_core.runnables import RunnableConfig
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.graph import END, StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt.tool_node import ToolNode
import numexpr
from typing_extensions import TypedDict
@tool
def calculator(expression: str) -> str:
\"\"\"Calculate expression using Python's numexpr library.
Expression should be a single line mathematical expression
that solves the problem.
```
Examples:
"37593 * 67" for "37593 times 67"
"37593**(1/5)" for "37593^(1/5)"
\"\"\"
local_dict = {"pi": math.pi, "e": math.e}
return str(
numexpr.evaluate(
expression.strip(),
global_dict={}, # restrict access to globals
local_dict=local_dict, # add common mathematical functions
)
)
model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
tools = [calculator]
model_with_tools = model.bind_tools(tools, tool_choice="any")
class ChainState(TypedDict):
\"\"\"LangGraph state.\"\"\"
messages: Annotated[Sequence[BaseMessage], add_messages]
async def acall_chain(state: ChainState, config: RunnableConfig):
last_message = state["messages"][-1]
response = await model_with_tools.ainvoke(state["messages"], config)
return {"messages": [response]}
async def acall_model(state: ChainState, config: RunnableConfig):
response = await model.ainvoke(state["messages"], config)
return {"messages": [response]}
graph_builder = StateGraph(ChainState)
graph_builder.add_node("call_tool", acall_chain)
graph_builder.add_node("execute_tool", ToolNode(tools))
graph_builder.add_node("call_model", acall_model)
graph_builder.set_entry_point("call_tool")
graph_builder.add_edge("call_tool", "execute_tool")
graph_builder.add_edge("execute_tool", "call_model")
graph_builder.add_edge("call_model", END)
chain = graph_builder.compile()
Extends
Source
Frequently Asked Questions
What is the LLMMathChain class?
LLMMathChain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/llm_math/base.py.
Where is LLMMathChain defined?
LLMMathChain is defined in libs/langchain/langchain_classic/chains/llm_math/base.py at line 33.
What does LLMMathChain extend?
LLMMathChain extends Chain.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free