Skip to content
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

[bug][anthropic] Tool result content is not added to spans #1109

Open
cephalization opened this issue Nov 8, 2024 · 0 comments
Open

[bug][anthropic] Tool result content is not added to spans #1109

cephalization opened this issue Nov 8, 2024 · 0 comments
Assignees
Labels
bug Something isn't working language: python Related to Python integration

Comments

@cephalization
Copy link
Contributor

cephalization commented Nov 8, 2024

The anthropic instrumentor does not capture tool_result content from user messages.

This means that you cannot view the results of a tool_use in phoenix without viewing the raw input json.

Trying to record a span of a message with the following shape:

tool_use_message: MessageParam = {
    "role": "user",
    "content": [
        {
            "type": "tool_result",
            "tool_use_id": tool.id,
            "content": [{"type": "text", "text": result}],
        },
    ],
}

Results in the following error:

Invalid type dict in attribute 'llm.input_messages.3.message.content' value sequence. Expected one of ['bool', 'str', 'bytes', 'int', 'float'] or None
Reproduction Example
  • create .env with ANTHROPIC_API_KEY value configured
  • Create file hello.py
import random
from anthropic import Anthropic
from anthropic.types.tool_param import ToolParam
from anthropic.types.message_param import MessageParam
from anthropic.types.model_param import ModelParam
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

from openinference.instrumentation.anthropic import AnthropicInstrumentor

from dotenv import load_dotenv

load_dotenv()

endpoint = "http://127.0.0.1:4317"
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
AnthropicInstrumentor().instrument(tracer_provider=tracer_provider)

client = Anthropic()
MODEL: ModelParam = "claude-3-5-haiku-latest"

weather_bot_prompt = """
I am a helpful weather bot. Provide me with a location and units,
and I will output the current weather.
"""
question = "what is the weather in california in imperial units?"

messages: list[MessageParam] = [
    {"role": "assistant", "content": weather_bot_prompt},
    {"role": "user", "content": question},
]

# Define the tool as a JSON schema string for Claude
tools: list[ToolParam] = [
    {
        "name": "get_weather",
        "description": "Get the current weather for a given location and units",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {"type": "string"},
                "units": {"type": "string", "enum": ["metric", "imperial"]},
            },
            "required": ["location", "units"],
        },
    }
]


message = client.messages.create(
    model=MODEL,
    messages=messages,
    tools=tools,
    max_tokens=1024,
)


def get_weather(location: str, units: str) -> str:
    return f"The weather in {location} is {random.randint(0, 100)} degrees {units}"


assert message.stop_reason == "tool_use"

tool = next(tool for tool in message.content if tool.type == "tool_use")
result: str = get_weather(
    location=tool.input["location"],  # type: ignore
    units=tool.input["units"],  # type: ignore
)

tool_use_message: MessageParam = {
    "role": "user",
    "content": [
        {
            "type": "tool_result",
            "tool_use_id": tool.id,
            "content": [{"type": "text", "text": result}],
        },
    ],
}

new_messages: list[MessageParam] = messages + [
    {"role": message.role, "content": message.content},
    tool_use_message,
]

response = client.messages.create(
    model=MODEL,
    messages=new_messages,
    max_tokens=1024,
    tools=tools,
)

res_content = response.content[0]

if res_content.type == "text":
    print(res_content.text)

print(f"\n\nMessages used: {new_messages}")
  • uv run hello.py
    • This will install deps automatically
  • View trace in "default" project of phoenix
  • Observe missing tool_result content in span
@github-project-automation github-project-automation bot moved this to 📘 Todo in phoenix Nov 8, 2024
@dosubot dosubot bot added bug Something isn't working language: python Related to Python integration labels Nov 8, 2024
@cephalization cephalization changed the title [bug][anthropic] Tool use content is not added to spans [bug][anthropic] Tool result content is not added to spans Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working language: python Related to Python integration
Projects
Status: 👨‍💻 In progress
Development

No branches or pull requests

2 participants