courses icon indicating copy to clipboard operation
courses copied to clipboard

In anthropic_api_fundamentals/05.Streaming.ipynb error: unexpecter keyword argument 'event_handler'

Open avadon1 opened this issue 1 year ago • 2 comments
trafficstars

Path to course in question: https://github.com/anthropics/courses/blob/master/anthropic_api_fundamentals/05_Streaming.ipynb In the Streaming helpers part of the 05.Streaming segment of the api_fundamentals courses when running the last piece of code I am getting the following error: image I tried downgrading to an earlier version of anthropic (0.33.0) but the problem persists.

avadon1 avatar Aug 28 '24 09:08 avadon1

This content should be updated to use the new Iterator API: https://github.com/anthropics/anthropic-sdk-python/pull/532

antsant avatar Sep 02 '24 17:09 antsant

I believe this is the current way to do this:

from anthropic import AsyncAnthropic

client = AsyncAnthropic()

green = '\033[32m'
reset = '\033[0m'

async def streaming_events_demo():
    async with client.messages.stream(
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": "Generate a 5-word poem",
            }
        ],
        model="claude-3-opus-20240229",
    ) as stream:
        async for event in stream:
            if event.type == "text":
                # This runs only on text delta stream messages
                print(green + event.text + reset, flush=True)
            else:
                # This runs on any stream event
                print("on_event fired:", event.type)

await streaming_events_demo()

antsant avatar Sep 02 '24 17:09 antsant

I needed to update to a newer opus version, but confirm this fix worked for me and produced the correct output.

SteveLeve avatar Nov 07 '25 21:11 SteveLeve