Python 88.3%
TypeScript 7.6%
Shell 4.1%
1#!/usr/bin/env python32"""Stream a tool call and print every input_json_delta fragment. eager_input_streaming=True enables fine-grained streaming (no buffering /3JSON validation) for that tool; guard the final json.loads. STATUS: LIVE_VERIFIED 2026-09-18 (b1 buffered 6 fragments, b2 eager 4 fragments).4Run: .venv/bin/python examples/anthropic/tools/fine-grained-streaming/basic.py [--buffered]5"""6import json, sys7from pathlib import Path8sys.path.insert(0, str(Path(__file__).resolve().parents[3]))9from scripts.live import anthropic_request10eager = "--buffered" not in sys.argv11tool = {"name": "get_weather", "description": "Get the current weather for a city.", "eager_input_streaming": eager,12 "input_schema": {"type": "object", "properties": {"location": {"type": "string"}}, "required": ["location"]}}13st, lines, _ = anthropic_request("POST", "/v1/messages", {"model": "claude-haiku-4-5-20251001", "max_tokens": 150, "stream": True, "tools": [tool],14 "tool_choice": {"type": "any"}, "messages": [{"role": "user", "content": "Weather in San Francisco, CA?"}]},15 stream=True, note=f"fine-grained-streaming eager={eager}")16buf = ""17for ln in lines:18 if not ln.startswith("data: "):19 continue20 ev = json.loads(ln[6:])21 if ev["type"] == "content_block_start":22 print("start:", ev["content_block"])23 elif ev["type"] == "content_block_delta" and ev["delta"]["type"] == "input_json_delta":24 frag = ev["delta"]["partial_json"]; buf += frag; print("fragment:", repr(frag))25 elif ev["type"] == "content_block_stop":26 try:27 print("parsed input:", json.loads(buf))28 except json.JSONDecodeError: # possible with eager streaming / max_tokens cut-off -> return {"INVALID_JSON": buf} as is_error tool_result29 print("INVALID_JSON:", buf)30