langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
91.87k stars 14.59k forks source link

Error in LangChainTracer.on_tool_end callback #22696

Open saurabhlalsaxena opened 2 months ago

saurabhlalsaxena commented 2 months ago

Checked other resources

Example Code

'''python

rag_agent_creation.py

from langchain.agents import AgentExecutor, create_openai_tools_agent from langchain_core.messages import BaseMessage, HumanMessage from langchain_openai import ChatOpenAI from langchain.tools.retriever import create_retriever_tool from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder from .rag_prompts import RAG_AGENT_PROMPT import chromadb

def create_retriver_agent(llm: ChatOpenAI, vectordb: chromadb):

retriever = vectordb.as_retriever(search_type = "mmr", search_kwargs={"k": 4})

retriever_tool = create_retriever_tool(
    retriever,
    name = "doc_retriever_tool",
    description = "Search and return information from documents",
)

tools = [retriever_tool]

system_prompt = RAG_AGENT_PROMPT

prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            system_prompt,
        ),
        MessagesPlaceholder(variable_name="messages",optional=True),
        HumanMessagePromptTemplate.from_template("{input}"),
        MessagesPlaceholder(variable_name="agent_scratchpad"),
    ]
)

agent = create_openai_tools_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
return executor
'''

Error Message and Stack Trace (if applicable)

Error in LangChainTracer.on_tool_end callback: TracerException("Found chain run at ID 879e1607-f32b-4984-af76-d258c646e7ad, but expected {'tool'} run.")

Description

I am using a retriever tool in a langgraph deployed on langserve. Whenever the graph calls the tool, i am getting the error: Error in LangChainTracer.on_tool_end callback: TracerException("Found chain run at ID 879e1607-f32b-4984-af76-d258c646e7ad, but expected {'tool'} run.") This is new, my tool was working correctly before. I have updated the dependencies as well.

System Info

[tool.poetry] name = "Reporting Tool API" version = "0.1.0" description = "" authors = ["Your Name you@example.com"] readme = "README.md" packages = [{ include = "app" }]

[tool.poetry.dependencies] python = "^3.11" uvicorn = "0.23.2" langserve = { extras = ["server"], version = "0.2.1" } pydantic = "<2" chromadb = "0.5.0" fastapi = "0.110.3" langchain = "0.2.3" langchain-cli = "0.0.24" langchain-community = "0.2.4" langchain-core = "0.2.5" langchain-experimental = "0.0.60" langchain-openai = "0.1.8" langchain-text-splitters = "0.2.1" langgraph = "0.0.65" openai = "1.33.0" opentelemetry-instrumentation-fastapi = "0.46b0" pypdf = "4.2.0" python-dotenv = "1.0.1" python-multipart = "0.0.9" pandas = "^2.0.1" tabulate = "^0.9.0" langchain-anthropic = "0.1.15" langchain-mistralai = "0.1.8" langchain-google-genai = "1.0.6" api-analytics = { extras = ["fastapi"], version = "*" } langchainhub = "0.1.18"

[tool.poetry.group.dev.dependencies] langchain-cli = ">=0.0.15"

[build-system] requires = ["poetry-core"] build-backend = "poetry.core.masonry.api"

keenborder786 commented 2 months ago

I tried the following code on my machine and faced no errors:


os.environ['OPENAI_API_KEY']=''

def create_retriver_agent(llm, vectordb):

    retriever = vectordb.as_retriever(search_type = "mmr", search_kwargs={"k": 4})

    retriever_tool = create_retriever_tool(
        retriever,
        name = "doc_retriever_tool",
        description = "Search and return information from documents",
    )

    tools = [retriever_tool]

    system_prompt = "You are expert in retriving documents"

    prompt = ChatPromptTemplate.from_messages(
        [
            (
                "system",
                system_prompt,
            ),
            HumanMessagePromptTemplate.from_template("{input}"),
            MessagesPlaceholder(variable_name="agent_scratchpad"),
        ]
    )

    agent = create_openai_tools_agent(llm, tools, prompt)
    executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
    return executor

from langchain_community.document_loaders import TextLoader
from langchain_community.embeddings.sentence_transformer import (
    SentenceTransformerEmbeddings,
)
from langchain_text_splitters import CharacterTextSplitter

# load the document and split it into chunks
loader = TextLoader("state_of_the_union.txt")
documents = loader.load()

# split it into chunks
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

executor = create_retriver_agent(ChatOpenAI(),Chroma.from_documents(docs, OpenAIEmbeddings()))
executor.invoke({'input':'Who is Danielle?'})

This mean the error is most likely due to langserve. Can you please update to v0.2.2 for langserve that might resolve the issue.

tonymm55 commented 2 months ago

I'm getting a similar error, but am not using LangServe, Error in LangChainTracer.on_tool_end callback: TracerException("Found chain run at ID 5f21a1b9-47b6-4fec-a1e5-b56b43b7ffe9, but expected {'tool'} run.")
Is this a problem, as my app is running OK. I was just curious as to where the issue lies. I am using LangGraph.

nanwang-crea commented 2 months ago

I'm getting a similar error, but am not using LangServe, Error in LangChainTracer.on_tool_end callback: TracerException("Found chain run at ID 5f21a1b9-47b6-4fec-a1e5-b56b43b7ffe9, but expected {'tool'} run.") Is this a problem, as my app is running OK. I was just curious as to where the issue lies. I am using LangGraph.

I'm getting a similar error, but am not using LangServe, Error in LangChainTracer.on_tool_end callback: TracerException("Found chain run at ID 5f21a1b9-47b6-4fec-a1e5-b56b43b7ffe9, but expected {'tool'} run.") Is this a problem, as my app is running OK. I was just curious as to where the issue lies. I am using LangGraph.

I'm getting a similar error, but am not using LangServe, Error in LangChainTracer.on_tool_end callback: TracerException("Found chain run at ID 5f21a1b9-47b6-4fec-a1e5-b56b43b7ffe9, but expected {'tool'} run.") Is this a problem, as my app is running OK. I was just curious as to where the issue lies. I am using LangGraph.

I also getting a similar error,could you tell me how to solve it? I'm not using LangServe or LangGraph, just create a Agent and run.

tonymm55 commented 2 months ago

For me. I just updated LangSmith to the latest version, and the error went away. It’s a bug that’s been fixed in the update. Worked for me anyway.

NicolasCampana commented 1 month ago

I was having the same problem using langsmith==0.1.75, after updating to langsmith==0.1.88 it seems that disappeared.