MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / main

Function main

examples/messages_stream.py:8–31  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6
7
8async def main() -> None:
9 async with client.messages.stream(
10 max_tokens=1024,
11 messages=[
12 {
13 "role": "user",
14 "content": "Say hello there!",
15 }
16 ],
17 model="claude-3-5-sonnet-latest",
18 ) as stream:
19 async for event in stream:
20 if event.type == "text":
21 print(event.text, end="", flush=True)
22 elif event.type == "content_block_stop":
23 print()
24 print("\ncontent block finished accumulating:", event.content_block)
25 print()
26
27 # you can still get the accumulated final message outside of
28 # the context manager, as long as the entire stream was consumed
29 # inside of the context manager
30 accumulated = await stream.get_final_message()
31 print("accumulated message: ", accumulated.to_json())
32
33
34asyncio.run(main())

Callers 1

messages_stream.pyFile · 0.70

Calls 3

to_jsonMethod · 0.80
streamMethod · 0.45
get_final_messageMethod · 0.45

Tested by

no test coverage detected