langchain-ai / langchain

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

[Community] Tavily Search lost Tool output Callbacks in newest versions #23632

Open zdql opened 2 months ago

zdql commented 2 months ago

Checked other resources

Example Code

We recently upgraded our libaries as follows:

-langchain = "0.2.0rc2"
+langchain = "^0.2.5"

-langchain-community = "0.2.0rc1"
+langchain-community = "^0.2.5"

-langchain-anthropic = "^0.1.9"
+langchain-anthropic = "^0.1.15"

-langchain-groq = "0.1.3"
+langchain-groq = "^0.1.5"

-langchain-core = "^0.1.52"
+langchain-core = "^0.2.9"

-langgraph = "^0.0.38"
+langgraph = "^0.0.69"

And have lost the ability to view the Tavily Search tool call's output in our callback handler.

When we revert packages, the tool output appears again. Our custom callback handler is able to output the Tool call output with every other tool in our stack after the update.

We implement the tavily tool like so:

from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.utilities.tavily_search import TavilySearchAPIWrapper

def get_tavily_search_tool(callbacks=[]):
    search = TavilySearchResults(
        name="search",
        api_wrapper=TavilySearchAPIWrapper(tavily_api_key="tvly-"), 
            callbacks=callbacks, 
            max_results=5
            )
    return search

And our callback handler looks something like this:

  async def on_tool_start(
        self,
        serialized: Dict[str, Any],
        input_str: str,
        **kwargs: Any,
    ) -> None:
        tool_spec = flatten_dict(serialized)

        tool_name = tool_spec.get("name", "Unknown Tool")
        tool_description = tool_spec.get("description", "No description available")
        self.queue.put_nowait(f"\n\nUsing `{tool_name}` (*{tool_description}*)\n")

    async def on_tool_end(
        self,
        output: str,
        color: Optional[str] = None,
        observation_prefix: Optional[str] = None,
        llm_prefix: Optional[str] = None,
        **kwargs: Any,
    ) -> None:
        """If not the final action, print out observation."""
        if observation_prefix is not None:
            self.queue.put_nowait(f"\n{observation_prefix}\n")

        if len(output) > 10000:
            # truncate output to 10.000 characters
            output = output[:10000]
            output += " ... (truncated to 10,000 characters)"
            self.queue.put_nowait(f"\n```json\n{output}\n```\n\n")
        else:
            if isinstance(output, dict):
                pretty_output = json.dumps(output, indent=4)
                self.queue.put_nowait(f"\n```json\n{pretty_output}\n```\n\n")

            elif isinstance(output, str):
                # attempt to parse the output as json
                try:
                    pretty_output = json.dumps(ast.literal_eval(output), indent=4)
                    self.queue.put_nowait(f"\n```json\n{pretty_output}\n```\n\n")
                except:
                    pretty_output = output
                    self.queue.put_nowait(f"\n```\n{pretty_output}\n```\n\n")

        if llm_prefix is not None:
            self.queue.put_nowait(f"\n{llm_prefix}\n")

Error Message and Stack Trace (if applicable)

No response

Description

We recently upgraded our libaries and have lost the ability to view the Tavily Search tool call's output in our callback handler. When we revert packages, the tool output appears again. Our custom callback handler is able to output the Tool call output with every other tool in our stack after the update.

System Info

langchain==0.1.16 langchain-anthropic==0.1.13 langchain-community==0.0.34 langchain-core==0.1.52 langchain-google-genai==1.0.3 langchain-mistralai==0.1.8 langchain-openai==0.1.6 langchain-text-splitters==0.0.1

eyurtsev commented 2 months ago

@zdql the original issue you posted had your API key on it. I modified the issue to erase it, but you should assume that the key is compromised and rotate it.


For the issue could you confirm that the search tool itself is working and that the issue is specifically with callbacks?

zdql commented 2 months ago

For the issue could you confirm that the search tool itself is working and that the issue is specifically with callbacks?

Confirmed. The Search tool itself seems to function as normal, just without callbacks as it had previously.