Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: pass tool_result is_error through to the model on /v1/messages (#11962)

Nilay324f4c0146df
diff --git a/studio/backend/core/inference/anthropic_compat.py b/studio/backend/core/inference/anthropic_compat.py
index 77efdc57cb7..0e140b6351d 100644
--- a/studio/backend/core/inference/anthropic_compat.py
+++ b/studio/backend/core/inference/anthropic_compat.py
@@ -114,6 +114,14 @@ def _anthropic_image_block_to_openai_part(block: dict) -> Optional[dict]:
return None
+def _tool_error_content(content: Union[str, list]) -> Union[str, list]:
+ if isinstance(content, list):
+ return [{"type": "text", "text": "Error:"}, *content]
+ if not content:
+ return "Error: tool returned no content"
+ return content if content.startswith("Error:") else f"Error: {content}"
+
+
def anthropic_messages_to_openai(
messages: list[dict],
system: Optional[Union[str, list]] = None,
@@ -236,6 +244,8 @@ def anthropic_messages_to_openai(
if any(p["type"] == "image_url" for p in parts)
else "\n".join(p["text"] for p in parts)
)
+ if b.get("is_error"):
+ tc = _tool_error_content(tc)
tool_results.append(
{
"role": "tool",
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index 5c68f364a8d..b1147b74b15 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -3369,6 +3369,7 @@ class AnthropicToolResultBlock(BaseModel):
type: Literal["tool_result"]
tool_use_id: str
content: Union[str, list] = ""
+ is_error: Optional[bool] = None
@field_validator("content", mode = "before")
@classmethod
diff --git a/studio/backend/tests/test_anthropic_messages.py b/studio/backend/tests/test_anthropic_messages.py
index 1e333224f48..012c5229a82 100644
--- a/studio/backend/tests/test_anthropic_messages.py
+++ b/studio/backend/tests/test_anthropic_messages.py
@@ -1041,6 +1041,57 @@ class TestAnthropicMessagesToOpenAI:
result = anthropic_messages_to_openai(msgs)
assert result[0]["content"] == "Line 1\nLine 2"
+ @pytest.mark.parametrize(
+ "content, expected",
+ [
+ ("permission denied", "Error: permission denied"),
+ ("Error: disk full", "Error: disk full"),
+ ("", "Error: tool returned no content"),
+ (None, "Error: tool returned no content"),
+ ([{"type": "text", "text": "exit 1"}], "Error: exit 1"),
+ ([], "Error: tool returned no content"),
+ ],
+ )
+ def test_tool_result_is_error_marks_the_tool_message(self, content, expected):
+ block = {"type": "tool_result", "tool_use_id": "tu_1", "is_error": True, "content": content}
+ request = AnthropicMessagesRequest(
+ max_tokens = 16, messages = [{"role": "user", "content": [block]}]
+ )
+ result = anthropic_messages_to_openai([m.model_dump() for m in request.messages])
+ assert result == [{"role": "tool", "tool_call_id": "tu_1", "content": expected}]
+
+ @pytest.mark.parametrize("flag", [{}, {"is_error": False}, {"is_error": None}])
+ @pytest.mark.parametrize("content", ["42", ""])
+ def test_tool_result_without_is_error_is_unchanged(self, flag, content):
+ block = {"type": "tool_result", "tool_use_id": "tu_1", "content": content, **flag}
+ request = AnthropicMessagesRequest(
+ max_tokens = 16, messages = [{"role": "user", "content": [block]}]
+ )
+ result = anthropic_messages_to_openai([m.model_dump() for m in request.messages])
+ assert result == [{"role": "tool", "tool_call_id": "tu_1", "content": content}]
+
+ def test_tool_result_is_error_with_image_prepends_the_marker(self):
+ image = {
+ "type": "image",
+ "source": {"type": "base64", "media_type": "image/png", "data": "AAAA"},
+ }
+ msgs = [
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "tu_1",
+ "is_error": True,
+ "content": [{"type": "text", "text": "crashed"}, image],
+ }
+ ],
+ }
+ ]
+ parts = anthropic_messages_to_openai(msgs)[0]["content"]
+ assert [p["type"] for p in parts] == ["text", "text", "image_url"]
+ assert [p["text"] for p in parts[:2]] == ["Error:", "crashed"]
+
def test_tool_result_search_results_keep_title_source_and_text(self):
msgs = [
{