8000 ToolUseBlock` object has no attribute 'text' error in Anthropic LLM Observability when using tool calls · Issue #238 · PostHog/posthog-python · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ToolUseBlock` object has no attribute 'text' error in Anthropic LLM Observability when using tool calls #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
LouisDelbosc opened this issue May 23, 2025 · 0 comments

Comments

@LouisDelbosc
Copy link

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

  • PostHog Python SDK version: 4.1.0
  • Python version: 3.12
  • Anthropic SDK version: 0.52.0

Steps to Reproduce

  1. Use the PostHog Anthropic client wrapper with tool calls enabled
  2. Make an API call that forces tool usage with tool_choice
  3. The response contains ToolUseBlock objects instead of text content

Code Example

from posthog.ai.anthropic import Anthropic

client = Anthropic(api_key=api_key, posthog_client=posthog)

response = client.messages.create(
    model="claude-3-5-haiku-20241022",
    system="Call the tool with your structured response.",
    messages=[{"role": "user", "content": "some message"}],
    max_tokens=8000,
    tools=[
        {
            "name": "respond",
            "description": "Provide structured response",
            "input_schema": {
                "type": "object",
                "properties": {"table": {"type": "array", "items": {...}}},
                "required": ["table"]
            },
        }
    ],
    tool_choice={"type": "tool", "name": "respond"},
)

Expected Behavior

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0