langchain-ai / langgraph

Build resilient language agents as graphs.
https://langchain-ai.github.io/langgraph/
MIT License
6.84k stars 1.1k forks source link

LangGraph issue for react agent tool artifact being streamed back in final response #2479

Closed tiaan720 closed 5 days ago

tiaan720 commented 5 days ago

Checked other resources

Example Code

import base64
import math
import os
import pprint
from math import cos, sin
from typing import Tuple

import mlflow
import numpy as np
import plotly.graph_objects as go
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.graph.graph import CompiledGraph
from langgraph.prebuilt import create_react_agent

os.environ["MLFLOW_TRACKING_URI"] = "http://localhost:5002"

mlflow.openai.autolog()

@tool
def calculate_area(radius: float) -> str:
   """
   Calculates the area of a circle given its radius.

   Args:
       radius (float): The radius of the circle.

   Returns:
       str: The area of the circle.
   """
   area = math.pi * (radius**2)
   return f"The area of the circle is {area:.2f}"

@tool(response_format="content_and_artifact")
def circle_plot(title: str, radius: float) -> Tuple[str, str]:
   """
   Plots a circle with a given radius and returns it to the user.

   Args:
       radius (float): The radius of the circle.

   Returns:
       Tuple[str, str]: A tuple containing the name of the plot and the base64-encoded image of the plot.
   """
   theta = np.linspace(0, 2 * np.pi, 100)
   x = radius * np.cos(theta)
   y = radius * np.sin(theta)

   fig = go.Figure()
   fig.add_trace(go.Scatter(x=x, y=y, mode="lines"))

   image: bytes = fig.to_image(format="png")
   image_base64 = f"data:image/png;base64,{base64.b64encode(image).decode("utf-8")}"
   return title, image_base64

if __name__ == "__main__":
   model = ChatOpenAI(
       model="gpt-4", temperature=0, max_tokens=None, max_retries=5, stop=None
   )
   tools = [circle_plot]
   agent: CompiledGraph = create_react_agent(
       model=model,
       tools=[circle_plot, calculate_area],
   )

   query = "What is the area of a circle with a radius of 5? Circle plot"
   input = {"messages": [{"role": "human", "content": query}]}

   for step in agent.stream(input, stream_mode="messages"):
       pprint.pprint(step)

Error Message and Stack Trace (if applicable)

"The area of the circle with a radius of 5 is 78.54. Here is the plot of the circle:\n\n![Circle with radius 5](data:image/png;base64,iVBORw0KGgo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29vc3Nzd3d3e3t7f39/g4ODh4eHi4uLj4+Pk5OTl5eXm5ubn5+fo6Ojp6enq6urp6enr6+vs7Ozt7e3u7u7v7+7w8PDx8fHy8vLz8/P09PT19fX29vb29vb39/f4+Pj5+fn6+vr7+/v8/Pz9/f3+/v7///+wsLCjo6OkpKSlpaWmpqanp6eoqKipqamqqqqrq6usrKytra2urq6vr6+wsLCxsbGysrKzs7O0tLS1tbW2tra3t7e4uLi5ubm6urq7u7u8vLy9vb2+vr6/v7/AwMDExMTFxcXGxsbHx8fIyMjJycnKysrLy8vMzMzNzc3Ozs7Pz8/Q0NDQ0NDR0dHS0tLT09PU1NTV1dXW1tbX19fY2NjZ2dnb29vc3Nzd3d3e3t7f39/g4ODh4eHi4uLj4+Pk5OTl5eXm5ubn5+fo6Ojp6enq6urp6enr6+vs7Ozt7e3u7u7v7+7w8PDx8fHy8vLz8/P09PT19fX29vb29vb39/f4+Pj5+fn6+vr7+/v8/Pz9/f3+/v7///+wsLCjo6OkpKSlpaWmpqanp6eoqKipqamqqqqrq6usrKytra2urq6vr6+wsLCxsbGysrKzs7O0tLS1tbW2tra3t7e4uLi5ubm6urq7u7u8vLy9vb2+vr6/v7/AwMDExMTFxcXGxsbHx8fIyMjJycnKysrLy8vMzMzNzc3Ozs7Pz8/Q0NDQ0NDR0dHS0tLT09PU1NTV1dXW1tbX19fY2NjZ2dnb29vc3Nzd3d3e3t7f39/g4ODh4eHi4uLj4+Pk5OTl5eXm5ubn5+fo6Ojp6enq6urp6enr6+vs7Ozt7e3u7u7v7+7w8PDx8fHy8vLz8/P09PT19fX29vb29vb39/f4+Pj5+fn6+vr7+/v8/Pz9/f3+/v7///+wsLCjo6OkpKSlpaWmpqanp6eoqKipqamqqqqrq6usrKytra2urq6vr6+wsLCxsbGysrKzs7O0tLS1tbW2tra3t7e4uLi5ubm6urq7u7u8vLy9vb2+vr6/v7/AwMDExMTF"

Description

We experienced in our code that the artifact output of the tool is streamed back into the end response. According to the documentation this should not be the case:

param artifact: Any = None# Artifact of the Tool execution which is not meant to be sent to the model. Should only be specified if it is different from the message content, e.g. if only a subset of the full tool output is being passed as message content but the full output is needed in other parts of the code.

We created an example python script to demonstrate this. This bug usually happens at random and for you to notice the issue you will have to try and rerun it a couple of times. If it doesnt work try setting it to gpt-4o, then run it and then set it back to gpt-4 and run it again. The issue didnt really occur with gpt-4o and its an edge case but it can cause alot of damage if it occurs. Obviously we can resolve it by simply using a different model and changing the prompt or prompting the artifact not to be streamed back. The core issue is still present that the artifact should never be fed back to the model. The tokens streamed back can occur high costs in a production environment. We also conducted mlflow traces with screenshots attached to demonstrate what is happening. We recommend running the code and one will see in the terminal what is happening as well.

image

image

System Info

mlflow==2.18.0 numpy==1.26.4 plotly==5.24.1 langchain-openai==0.2.1 langgraph==0.2.35 langchain==0.3.7

vbarda commented 5 days ago

@tiaan720 i am not sure why you're seeing any randomness in the results. i tested your example with gpt-4o and it works as i would expect: the artifact is never actually sent to the LLM but the tool message with artifact IS streamed from the graph

artifacts are handled internally by langchain and the LLM never sees them, so you can't control them via the prompts

tiaan720 commented 5 days ago

Good day @vbarda, thanks for the reply. This issue is that the output artifact from the tool should not be regurgitated back to the model in the final response. With randomness I implied that the bug doesn't always occur when running the code. We know that LLM's inherently have a randomness aspect to it, even thought temperature is set to zero. So maybe give it a couple of tries and see if you can get the bug to arise (remember to use gpt-4 as I mentioned in my previous response). If you cant get it to work I will try and capture screenshots or send a video.

vbarda commented 5 days ago

ah @tiaan720 i actually see the issue even with gpt-4o -- i think the model is just hallucinating the image data, so you would need to improve the prompt / tool descriptions. try replacing the return from circle_plot with fake image data

@tool(response_format="content_and_artifact")
def circle_plot(title: str, radius: float) -> Tuple[str, str]:
    ...
    return title, "foo"

i get this in response from gpt-4 when using a fake artifact.

{'agent': {'messages': [AIMessage(content='The area of the circle with a radius of 5 is 78.54. Here is a plot of the circle:\n\n![Circle with radius 5](data:image/png;base64,iVBORw0KGg...)', ...}

so i don't believe the artifact is actually being fed into the model or that there is any other bug in langgraph. will close for now but let me know if there is still an issue