ToolUseBlock` object has no attribute 'text' error in Anthropic LLM Observability when using tool calls · Issue #238 · PostHog/posthog-python · GitHub
More Web Proxy on the site http://driver.im/
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The PostHog AI observability feature fails when tracking Anthropic API responses that contain tool use blocks, causing an AttributeError: 'ToolUseBlock' object has no attribute 'text'.
Environment
PostHog Python SDK version: 4.1.0
Python version: 3.12
Anthropic SDK version: 0.52.0
Steps to Reproduce
Use the PostHog Anthropic client wrapper with tool calls enabled
Make an API call that forces tool usage with tool_choice
The response contains ToolUseBlock objects instead of text content
The PostHog wrapper should handle tool use responses correctly and track the usage without errors.
Actual Behavior
AttributeError: 'ToolUseBlock' object has no attribute 'text'
Full Traceback
File "/usr/local/lib/python3.12/site-packages/posthog/ai/utils.py", line 285, in call_llm_and_track_usage
ph_client, posthog_privacy_mode, format_response(response, provider)
File "/usr/local/lib/python3.12/site-packages/posthog/ai/utils.py", line 93, in format_response
return format_response_anthropic(response)
File "/usr/local/lib/python3.12/site-packages/posthog/ai/utils.py", line 102, in format_response_anthropic
if choice.text:
File "/usr/local/lib/python3.12/site-packages/pydantic/main.py", line 991, in __getattr__
raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'ToolUseBlock' object has no attribute 'text'
Root Cause
The issue is in the format_response_anthropic function in /posthog/ai/utils.py at line 102. The code assumes all content blocks have a text attribute, but ToolUseBlock objects don't have this attribute - they have type, name, and input attributes instead.
Impact
This breaks any usage of the PostHog Anthropic wrapper when using tool calls, which is a common pattern for structured output generation.
The text was updated successfully, but these errors were encountered:
Bug Report
Summary
The PostHog AI observability feature fails when tracking Anthropic API responses that contain tool use blocks, causing an
AttributeError: 'ToolUseBlock' object has no attribute 'text'
.Environment
Steps to Reproduce
tool_choice
ToolUseBlock
objects instead of text contentCode Example
Expected Behavior
The PostHog wrapper should handle tool use responses correctly and track the usage without errors.
Actual Behavior
Full Traceback
Root Cause
The issue is in the
format_response_anthropic
function in/posthog/ai/utils.py
at line 102. The code assumes all content blocks have atext
attribute, butToolUseBlock
objects don't have this attribute - they havetype
,name
, andinput
attributes instead.Impact
This breaks any usage of the PostHog Anthropic wrapper when using tool calls, which is a common pattern for structured output generation.
The text was updated successfully, but these errors were encountered: