griptape-ai / griptape

Modular Python framework for AI agents and workflows with chain-of-thought reasoning, tools, and memory.
https://www.griptape.ai
Apache License 2.0
2.02k stars 170 forks source link

Chat handle_output no longer accepts structures with Stream=True #1306

Closed shhlife closed 2 weeks ago

shhlife commented 2 weeks ago

Describe the bug In griptape v0.33 the Chat utility used to have an output_fn parameter that could handle agents that had a stream parameter set to true. For example:

from griptape.structures import Agent
from griptape.utils import Chat

def output_fn(text: str):
  print(text, end="", flush=True)

agent = Agent(stream=True)
Chat(agent, output_fn=output_fn).start()

With the latest update to v0.34, output_fn has been renamed to handle_output, and it no longer accepts stream.

from griptape.structures import Agent
from griptape.utils import Chat

def output_fn(text: str):
  print(text, end="", flush=True)

agent = Agent(stream=True)
Chat(agent, handle_output=output_fn).start()

gives this result:

Traceback (most recent call last):
  File "c:\Users\jason\Documents\GitHub\griptape-intro-demos\2b_demo_chat_llama.py", line 19, in <module>      
    ).start()
      ^^^^^^^
  File "C:\Users\jason\Documents\GitHub\griptape-intro-demos\.venv\Lib\site-packages\griptape\utils\chat.py", line 83, in start
    self.handle_output(self.response_prefix + first_chunk.value, stream=True)
TypeError: output_fn() got an unexpected keyword argument 'stream'

this can be resolved by adding a **kwarsg to the output_fn, but we should make sure to document this.

vachillo commented 2 weeks ago

can you try updating your output_fn to:

from griptape.structures import Agent
from griptape.utils import Chat

def output_fn(text: str, stream: bool):
  print(text, end="", flush=True)

agent = Agent(stream=True)
Chat(agent, handle_output=output_fn).start()

the error is happening because it is expecting your provided handle_output function to have a stream parameter, which is in the docstring

shhlife commented 2 weeks ago

Yeah, that's the first thing I tired but it still gave an error: TypeError: output_fn() missing 1 required positional argument: 'stream'

doing **kwargs was the only thing that stopped the error.

Also, it wasn't mentioned in the changelog.md file - would it be possible to add that since it's a breaking change for existing code?

vachillo commented 2 weeks ago

ah i see. can you try:

from griptape.structures import Agent
from griptape.utils import Chat

def output_fn(text: str, *, stream: bool = False):
  print(text, end="", flush=True)

agent = Agent(stream=True)
Chat(agent, handle_output=output_fn).start()

for now? the default function has the expected method signature:

default_handle_output(self, text: str, *, stream: bool = False)

but the docstring should probably be more specific I also dont think this was intended to be breaking based on the changelog, so it needs to be updated

shhlife commented 2 weeks ago

sadly, that didn't work either - same error :(

vachillo commented 2 weeks ago

sorry just updated, add the default value there. basically just copy the method signature from default_handle_output exactly. PR coming in for this too