Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Update streaming docs and debug logs

Abi Rajab84de96fe5bd
diff --git a/backend/agent/providers/anthropic/parser.py b/backend/agent/providers/anthropic/parser.py
index e4f2744c..28debc2a 100644
--- a/backend/agent/providers/anthropic/parser.py
+++ b/backend/agent/providers/anthropic/parser.py
@@ -1,9 +1,9 @@
import uuid
from dataclasses import dataclass, field
-from typing import Any, Dict, List
+from typing import Any, Dict, List, cast
from agent.providers.types import EventSink, StreamEvent
-from agent.tools import ToolCall
+from agent.tools import ToolCall, parse_json_arguments
@dataclass
@@ -83,11 +83,21 @@ def extract_tool_calls(final_message: Any) -> List[ToolCall]:
for block in final_message.content:
if block.type != "tool_use":
continue
+ raw_input = getattr(block, "input", {})
+ args: Dict[str, Any]
+ if isinstance(raw_input, dict):
+ args = cast(Dict[str, Any], raw_input)
+ else:
+ parsed, error = parse_json_arguments(raw_input)
+ if error:
+ args = {"INVALID_JSON": str(raw_input)}
+ else:
+ args = parsed
tool_calls.append(
ToolCall(
id=block.id,
name=block.name,
- arguments=block.input,
+ arguments=args,
)
)
return tool_calls
diff --git a/backend/agent/providers/anthropic/stream.py b/backend/agent/providers/anthropic/stream.py
index 99072128..ec19329a 100644
--- a/backend/agent/providers/anthropic/stream.py
+++ b/backend/agent/providers/anthropic/stream.py
@@ -34,14 +34,4 @@ def create_stream_context(
else:
stream_kwargs["temperature"] = 0.0
- if hasattr(client, "beta") and hasattr(client.beta, "messages"):
- stream_client = client.beta.messages
- else:
- stream_client = client.messages
-
- try:
- stream_kwargs_with_betas = dict(stream_kwargs)
- stream_kwargs_with_betas["betas"] = ["fine-grained-tool-streaming-2025-05-14"]
- return stream_client.stream(**stream_kwargs_with_betas)
- except TypeError:
- return stream_client.stream(**stream_kwargs)
+ return client.messages.stream(**stream_kwargs)
diff --git a/backend/agent/tools.py b/backend/agent/tools.py
index 5122accc..b9ccfaab 100644
--- a/backend/agent/tools.py
+++ b/backend/agent/tools.py
@@ -345,6 +345,7 @@ def serialize_anthropic_tools(
{
"name": tool.name,
"description": tool.description,
+ "eager_input_streaming": True,
"input_schema": _copy_schema(tool.parameters),
}
for tool in tools
@@ -437,6 +438,17 @@ class AgentToolbox:
self.option_codes = option_codes or []
async def execute(self, tool_call: ToolCall) -> ToolExecutionResult:
+ if "INVALID_JSON" in tool_call.arguments:
+ invalid_json = ensure_str(tool_call.arguments.get("INVALID_JSON"))
+ return ToolExecutionResult(
+ ok=False,
+ result={
+ "error": "Tool arguments were invalid JSON.",
+ "INVALID_JSON": invalid_json,
+ },
+ summary={"error": "Invalid JSON tool arguments"},
+ )
+
if tool_call.name == "create_file":
return self._create_file(tool_call.arguments)
if tool_call.name == "edit_file":