LangChain Sequential Thinking Tool
This package provides a LangChain BaseTool implementation based on the sequential thinking pattern, inspired by the Model Context Protocol (MCP) server implementation. It allows language models to structure their problem-solving process into explicit, potentially revisable thought steps.
Features
- Structured Thinking: Guides LLMs to break down problems into numbered thoughts.
- State Management: Tracks thought history and allows for branching/revisions (managed within the tool instance).
- Rich Output: Displays thoughts and recommendations in the console using
richfor better visibility during agent execution. - LangChain Integration: Designed to be used seamlessly within LangChain agents.
- Pydantic Validation: Uses Pydantic for robust input validation.
Installation
pip install langchain-sequential-thinking-tool
Usage
Instantiate the tool and include it in your agent's tool list. The agent's LLM should be prompted to use this tool for complex reasoning tasks, providing the required fields (thought, thought_number, total_thoughts, next_thought_needed) and optional fields (is_revision, current_step, etc.) as needed.
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_react_agent
from langchain_core.prompts import PromptTemplate
from sequential_thinking_tool import SequentialThinkingTool # Assuming package is installed
# 1. Initialize LLM and Tool
llm = ChatOpenAI(model="gpt-4o", temperature=0)
thinking_tool = SequentialThinkingTool()
tools = [thinking_tool]
# 2. Define Agent Prompt (example)
# Ensure your prompt instructs the LLM on HOW and WHEN to use the sequential_thinking_tool
prompt_template = """
Answer the following questions as best you can. You have access to the following tools:
{tools}
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do. For complex problems, use the sequential_thinking_tool to break down your reasoning step-by-step. Structure your thoughts clearly.
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action, as a dictionary. For sequential_thinking_tool, provide 'thought', 'thought_number', 'total_thoughts', 'next_thought_needed', and optional fields like 'current_step'.
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer.
Final Answer: the final answer to the original input question
Begin!
Question: {input}
{agent_scratchpad}
"""
prompt = PromptTemplate.from_template(prompt_template)
# 3. Create Agent
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# 4. Run the Agent
try:
# Example task requiring sequential thought
task = "Plan the steps to build a simple web server using Python Flask, including setting up the environment, writing basic code, and running it."
result = agent_executor.invoke({"input": task})
print("\nFinal Result:")
print(result.get("output"))
# You can inspect the tool's history after execution
# print("\nTool History:")
# print(json.dumps(thinking_tool.get_history(), indent=2))
except Exception as e:
print(f"An error occurred: {e}")
How it Works
The tool receives a dictionary matching the ThoughtDataInput schema. It validates the input, updates its internal history (stored in the thought_history list within the tool instance), formats the thought using rich, prints it to stderr, and returns a summary dictionary to the agent.
Note: Unlike the original MCP server, this tool does not automatically analyze other available tools. Any tool recommendations within the current_step field must be generated by the LLM itself based on its knowledge or the context provided in the prompt/history.
Development
- Clone the repository.
- Create a virtual environment:
python -m venv .venv - Activate:
source .venv/bin/activate(or.\.venv\Scripts\activateon Windows) - Install dependencies:
pip install -e ".[dev]"(Assuming a[project.optional-dependencies]section for dev tools likepytest,build,twineis added topyproject.toml) - Run tests:
pytest
Publishing to PyPI
This project uses GitHub Actions with OIDC for trusted publishing to the Python Package Index (PyPI).
The publishing process is defined in the .github/workflows/publish-to-pypi.yml workflow file. Creating a new GitHub release will automatically trigger this workflow, building the package and uploading it to PyPI.
For more details on how trusted publishing works, refer to the PyPI documentation on trusted publishers.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
Metadata
Release files for langchain-sequential-thinking-tool 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langchain_sequential_thinking_tool-0.1.1.tar.gz | 10.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_sequential_thinking_tool-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.3 kB
Release files / langchain_sequential_thinking_tool-0.1.1.tar.gz
| Download URL | langchain_sequential_thinking_tool-0.1.1.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a8493433f3f831b1b01072f853b5f8658adacfd125b344b13c601cfd76cdc52d
|
|
BLAKE2b-256 checksum How to use checksums |
dab0b5471b856a8be6506a341582fb0b0ea12f2f61823a92862586347611dd1d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 3, 2025.
Transparency logRelease files / langchain_sequential_thinking_tool-0.1.1-py3-none-any.whl
| Download URL | langchain_sequential_thinking_tool-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d6e7e625ba3eea535d18ac26fee0dc8ce95f2297c12961baf67549e020d09054
|
|
BLAKE2b-256 checksum How to use checksums |
6305cdbcf159cd0b725ff2843a2d27f9fc366c6ca5df096c32a05cb499bfaae7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 3, 2025.
Transparency log