Skip to content

ToolResultBlock

Base classes

Name Children Inherits
BaseContentBlock
clawd_code_sdk.models.content_blocks
Shared base for all content block types.

⋔ Inheritance diagram

graph TD
  43398481["content_blocks.ToolResultBlock"]
  2489835100["content_blocks.BaseContentBlock"]
  3879467795["main.BaseModel"]
  2751680898["builtins.object"]
  2489835100 --> 43398481
  3879467795 --> 2489835100
  2751680898 --> 3879467795

🛈 DocStrings

Bases: BaseContentBlock

Tool result content block.

Source code in src/clawd_code_sdk/models/content_blocks.py
class ToolResultBlock(BaseContentBlock):
    """Tool result content block."""

    type: Literal["tool_result"] = "tool_result"
    tool_use_id: str = ""
    content: str | list[dict[str, Any]] | None = None  # BetaContentBlock
    is_error: bool | None = None

    def get_parsed_content(self) -> list[ToolResultContentBlock] | str | None:
        # TODO: or is it anthropic.types.beta.beta_tool_result_block_param.Content?
        from clawd_code_sdk.models.anthropic_types import validate_tool_result_content

        if self.content is None or isinstance(self.content, str):
            return self.content
        # Validate list content against Anthropic SDK types
        return validate_tool_result_content(self.content)

    def extract_text(self) -> str:
        """Extract text content from this tool result."""
        match self.content:
            case None:
                return ""
            case str():
                return self.content
            case list():
                text_parts = [tc.get("text", "") for tc in self.content if tc.get("type") == "text"]
                return "\n".join(text_parts)
            case _ as unreachable:
                assert_never(unreachable)

    def to_otel(self) -> ToolCallResponsePart:
        from logfire._internal.integrations.llm_providers.semconv import ToolCallResponsePart

        res = {"content": self.content} if isinstance(self.content, list) else self.content
        return ToolCallResponsePart(
            type="tool_call_response", id=self.tool_use_id or "", response=res
        )

extract_text

extract_text() -> str

Extract text content from this tool result.

Source code in src/clawd_code_sdk/models/content_blocks.py
def extract_text(self) -> str:
    """Extract text content from this tool result."""
    match self.content:
        case None:
            return ""
        case str():
            return self.content
        case list():
            text_parts = [tc.get("text", "") for tc in self.content if tc.get("type") == "text"]
            return "\n".join(text_parts)
        case _ as unreachable:
            assert_never(unreachable)

:fa-brands-github: Show source on GitHub