Home / File/ web_search_stream.py — anthropic-sdk-python Source File

web_search_stream.py — anthropic-sdk-python Source File

Architecture documentation for web_search_stream.py, a python file in the anthropic-sdk-python codebase. 2 imports, 0 dependents.

File python AnthropicClient SyncAPI 2 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  1cb21b00_539d_e8e1_23bf_a71f7566dfa9["web_search_stream.py"]
  22e54b11_c61c_96dc_bf33_dfc7151750e9["asyncio"]
  1cb21b00_539d_e8e1_23bf_a71f7566dfa9 --> 22e54b11_c61c_96dc_bf33_dfc7151750e9
  d10c5377_2939_0f0b_cc44_8759393f2853["anthropic"]
  1cb21b00_539d_e8e1_23bf_a71f7566dfa9 --> d10c5377_2939_0f0b_cc44_8759393f2853
  style 1cb21b00_539d_e8e1_23bf_a71f7566dfa9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import asyncio

from anthropic import AsyncAnthropic


async def main() -> None:
    client = AsyncAnthropic()

    print("Claude with Web Search (Streaming)")
    print("==================================")

    # Create an async stream with web search enabled
    async with client.beta.messages.stream(
        model="claude-sonnet-4-5-20250929",
        max_tokens=1024,
        messages=[{"role": "user", "content": "What's the weather in New York?"}],
        tools=[
            {
                "name": "web_search",
                "type": "web_search_20250305",
            }
        ],
    ) as stream:
        # Process streaming events
        async for chunk in stream:
            # Print text deltas as they arrive
            if chunk.type == "content_block_delta" and chunk.delta.type == "text_delta":
                print(chunk.delta.text, end="", flush=True)

            # Track when web search is being used
            elif chunk.type == "content_block_start" and chunk.content_block.type == "web_search_tool_result":
                print("\n[Web search started...]", end="", flush=True)

            elif chunk.type == "content_block_stop" and chunk.content_block.type == "web_search_tool_result":
                print("[Web search completed]", end="\n\n", flush=True)

        # Get the final complete message
        message = await stream.get_final_message()

    print("\n\nFinal usage statistics:")
    print(f"Input tokens: {message.usage.input_tokens}")
    print(f"Output tokens: {message.usage.output_tokens}")

    if message.usage.server_tool_use:
        print(f"Web search requests: {message.usage.server_tool_use.web_search_requests}")
    else:
        print("No web search requests recorded in usage")

    # Rather than parsing the web search results structure (which varies),
    # we'll just show the complete message structure for debugging
    print("\nMessage Content Types:")
    for i, block in enumerate(message.content):
        print(f"Content Block {i + 1}: Type = {block.type}")

    # Show the entire message structure as JSON for debugging
    print("\nComplete message structure (JSON):")
    print(message.model_dump_json(indent=2))


if __name__ == "__main__":
    asyncio.run(main())

Subdomains

Functions

Dependencies

  • anthropic
  • asyncio

Frequently Asked Questions

What does web_search_stream.py do?
web_search_stream.py is a source file in the anthropic-sdk-python codebase, written in python. It belongs to the AnthropicClient domain, SyncAPI subdomain.
What functions are defined in web_search_stream.py?
web_search_stream.py defines 2 function(s): asyncio, main.
What does web_search_stream.py depend on?
web_search_stream.py imports 2 module(s): anthropic, asyncio.
Where is web_search_stream.py in the architecture?
web_search_stream.py is located at examples/web_search_stream.py (domain: AnthropicClient, subdomain: SyncAPI, directory: examples).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free