Langchain
docs.langchain.com › oss › python › integrations › tools › python
Python REPL integration - Docs by LangChain
# You can create the tool to pass to an agent @tool def python_repl_tool(code: str) -> str: """A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`. """ return python_repl.run(code) Edit this page on GitHub or file an issue.
Videos
GitHub
github.com › langchain-ai › langchain › issues › 10079
how to use PythonREPL tool to take dataframe and query · Issue #10079 · langchain-ai/langchain
September 1, 2023 - langchain-ai / langchain Public · Notifications · You must be signed in to change notification settings · Fork 20.6k · Star 125k · New issueCopy link · New issueCopy link · Closed as not planned · Closed as not planned · how to use PythonREPL tool to take dataframe and query#10079 · Copy link · bmshambu · opened · on Sep 1, 2023 · Issue body actions · I need to use Python REPL tool to take data frame and user query and answer based on the data frame.
Author bmshambu
LangChain
python.langchain.com › api_reference › experimental › tools › langchain_experimental.tools.python.tool.PythonREPLTool.html
PythonREPLTool — 🦜🔗 LangChain documentation
GitHub · X / Twitter · class langchain_experimental.tools.python.tool.PythonREPLTool[source]# Bases: BaseTool · Tool for running python code in a REPL. Initialize the tool. Raises: TypeError – If args_schema is not a subclass of pydantic BaseModel or dict.
GitHub
github.com › langchain-ai › langchain › issues › 33800
Add PythonREPLTool (formerly in langchain_experimental) support for LangChain v1 · Issue #33800 · langchain-ai/langchain
November 3, 2025 - Consistent Python code execution tools across agent frameworks (LangGraph, LangChain 1.x). Safer sandboxed execution aligned with current security guidelines. Simplified migration for users upgrading from the 0.3 series. Re-expose a REPL-style tool under the new unified API, e.g.:
Author jashwanthpriglobal
GitHub
github.com › langchain-ai › langchain › issues › 16067
how to save plots/graphs when using PythonREPLTool in langchain experimental · Issue #16067 · langchain-ai/langchain
January 16, 2024 - Use this more than the Python tool if the question " "is about employee data, like 'how many employees are there?' or 'count the number of department'" ) repl_tool = Tool( name="Data", # We'll just call it 'Data' func=db_chain.run, description=description, ) tools.append(repl_tool) prefix = "Fulfill the following request as best you can.
Author jyoti194
GitHub
github.com › langchain-ai › langchain › discussions › 22841
Extracting python code from PythonREPLTool · langchain-ai/langchain · Discussion #22841
June 13, 2024 - I searched the LangChain documentation with the integrated search. I used the GitHub search to find a similar question and didn't find it. ... @st.cache_resource def get_python_agent(query): tools = [PythonREPLTool()] instructions = """You are an agent designed to write and execute python code to answer questions. You have access to a python REPL, which you can use to execute python code.
Author langchain-ai
GitHub
github.com › botextractai › ai-langchain-react-agent
GitHub - botextractai/ai-langchain-react-agent: Create a LangChain ReAct agent with multiple tools (Python REPL and DuckDuckGo Search)
Create a LangChain ReAct agent with multiple tools (Python REPL and DuckDuckGo Search) - botextractai/ai-langchain-react-agent
Starred by 13 users
Forked by 2 users
Languages Python 100.0% | Python 100.0%
GitHub
github.com › langchain-ai › langgraph › issues › 2073
python REPL tool not working the for code execution · Issue #2073 · langchain-ai/langgraph
October 10, 2024 - from langchain_experimental.utilities import PythonREPL from langchain_core.tools import tool repl = PythonREPL() @tool def python_repl(code): """ Use this to execute python code without any kind of error. """ try: print("Result of code execution here") result = repl.run(code) print("Result of code execution",result) except BaseException as e: return f"Failed to execute.
Author pradeepdev-1995
GitHub
github.com › langchain-ai › langchain › pull › 23678
Added example usage for Python REPL tool by Robert-Jia00129 · Pull Request #23678 · langchain-ai/langchain
June 30, 2024 - agree with eugene re focusing on the tool itself. We've also come up with a standard tool integration format, could we follow that here? here's a good example of it: https://python.langchain.com/v0.2/docs/integrations/tools/tavily_search/
Author langchain-ai
GitHub
github.com › Ryota-Kawamura › LangChain-for-LLM-Application-Development › blob › main › L6-Agents.ipynb
LangChain-for-LLM-Application-Development/L6-Agents.ipynb at main · Ryota-Kawamura/LangChain-for-LLM-Application-Development
" \"Human: You are an agent designed to write and execute python code to answer questions.\\nYou have access to a python REPL, which you can use to execute python code.\\nIf you get an error, debug your code and try again.\\nOnly use the output of your code to answer the question.
Author Ryota-Kawamura
GitHub
github.com › langchain-ai › langchain › discussions › 16449
Python_repl syntax error · langchain-ai/langchain · Discussion #16449
llm_math = LLMMathChain.from_llm(ChatOpenAI()) python_repl = PythonREPL() tools = [ Tool( name="python_repl", description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.", func=python_repl.run ), ] llm = ChatOpenAI(temperature=0.9,model = "gpt-4") agent_= initialize_agent( tools, llm=llm, agent="zero-shot-react-description", verbose=True, handle_parsing_errors=True ) python_repl.run("3*4は?") "SyntaxError('invalid decimal literal', ('<string>', 1, 3, '3*4は?', 1, 3))"
Author langchain-ai
Top answer 1 of 2
2
I believe this code as printed in the book "Generative AI with LangChain" relies on and older version of langchain. langchain[docarray]==0.0.284 to be exact.
I suggest setting up a conda environment for the book as there seemed to be breaking changes.
If on the other hand you would want to use the newest LangChain version, this would work:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType, Tool
from langchain.utilities import PythonREPL
from langchain_experimental.tools import PythonREPLTool
agent = initialize_agent(
tools=[PythonREPLTool()],
llm=llm,
)
agent.invoke("what is 2 + 2?")ython_repl",
description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
func=python_repl.run,
)
agent = initialize_agent(
tools=[python_repl],
llm=llm,
)
agent.invoke("what is 2 + 2?")
2 of 2
0
If you check the source code of load_tools method, you can actually see that the allowed input names are defined inside the following dictionaries:
- _LLM_TOOLS
- _EXTRA_LLM_TOOLS
- _EXTRA_OPTIONAL_TOOLS
- DANGEROUS_TOOLS
It's correct that python_repl name is not recognized. However, you can actually use it as tool like the following:
import os
from langchain_openai import AzureChatOpenAI
from langchain_experimental.tools import PythonREPLTool
from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent
llm = AzureChatOpenAI(
openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
azure_deployment=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"],
model_version=os.environ["AZURE_OPENAI_MODEL_VERSION"]
)
tools = [PythonREPLTool()]
instructions = """You are an agent designed to write and execute python code to answer questions.
You have access to a python REPL, which you can use to execute python code.
If you get an error, debug your code and try again.
Only use the output of your code to answer the question.
You might know the answer without running any code, but you should still run the code to get the answer.
If it does not seem like you can write code to answer the question, just return "I don't know" as the answer.
"""
base_prompt = hub.pull("langchain-ai/react-agent-template")
prompt = base_prompt.partial(instructions=instructions)
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "What is the 10th fibonacci number?"})
Giving as output something like:
Entering new AgentExecutor chain...
Thought: Do I need to use a tool? Yes
Action: Python_REPL
Action Input:
```
def fibonacci(n):
if n<=1:
return n
else:
return fibonacci(n-1)+fibonacci(n-2)
print(fibonacci(9))
```Python REPL can execute arbitrary code. Use with caution.
34
Do I need to use a tool? No
Final Answer: The 10th fibonacci number is 55.
Finished chain.
LangChain
python.langchain.com › api_reference › experimental › utilities › langchain_experimental.utilities.python.PythonREPL.html
PythonREPL — 🦜🔗 LangChain documentation
GitHub · X / Twitter · class langchain_experimental.utilities.python.PythonREPL[source]# Bases: BaseModel · Simulates a standalone Python REPL. Create a new model by parsing and validating input data from keyword arguments. Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
GitHub
github.com › langchain-ai › langchain › issues › 8821
How to pass a CSV file or a dataframe to Pandas REPL tool in Langchain? · Issue #8821 · langchain-ai/langchain
August 6, 2023 - I am trying to utilize Python Repl Tool in langchain with a CSV file and send me the answer based on the CSV file content. The problem is that it gets the action_input step of writing the code to e...
Author moayadeldin
GitHub
github.com › langchain-ai › langchain › discussions › 11680
PythonRepl tools and Pandas/Xorbits/Spark/Python agents are moving to langchain_experimental · langchain-ai/langchain · Discussion #11680
Agents: Pandas Agent, Xorbits Agent, Spark Agent, Python Agent Toolkits: python Tools: PythonREPLTool, PythonAstREPLTool · We will make the relevant code available in langchain_experimental shortly, with final deprecation from langchain scheduled for 10/27/2023.
Author langchain-ai
GitHub
github.com › langchain-ai › langchain › issues › 586
Improve python repl tool · Issue #586 · langchain-ai/langchain
January 11, 2023 - langchain-ai / langchain Public · Notifications · You must be signed in to change notification settings · Fork 21.5k · Star 131k · New issueCopy link · New issueCopy link · Closed as not planned · Closed as not planned · Improve python repl tool#586 ·
Author Harrison Chase(hwchase17)