Skip to content

🐛 Bug Report: Anthropic instrumentation fails to serialize pydantic messages from previous assistant responses #3040

Closed
@dinmukhamedm

Description

@dinmukhamedm

Which component is this bug for?

Anthropic Instrumentation

📜 Description

When I send the content of a previous response as a history message instrumentation fails to add this to attributes

Very similar issue in the past with OpenAI: #2976

👟 Reproduction steps

image_content_block = {
    "type": "image",
    "source": {
        "type": "base64",
        "media_type": "image/jpeg",
        "data": base64.b64encode(
            open(
                Path(__file__).parent.joinpath("data/logo.jpg"),
                "rb",
            ).read()
        ).decode("utf-8"),
    },
}

client = Anthropic()
user_message1 = {
    "role": "user",
    "content": "Are you capable of describing an image?"
}
user_message2 = {
    "role": "user",
    "content": [
        {"type": "text", "text": "What do you see?"},
        image_content_block, # a base64 or url encoded image content
    ]
}

response1 = client.messages.create(
    max_tokens=1024,
    model="claude-3-5-haiku-latest",
    messages=[
        user_message1,
    ],
)

response2 = client.messages.create(
    max_tokens=1024,
    model="claude-3-5-haiku-latest",
    messages=[
        user_message1,
        {"role": "assistant", "content": response1.content},
        user_message2,
    ],
)

👍 Expected behavior

For the second span,

gen_ai.prompt.2.role=user
gen_ai.prompt.2.content='{"type": "text", "text": "What do you see?"}'

👎 Actual Behavior with Screenshots

There's no attributes for the last message on the second span

🤖 Python Version

3.13

📃 Provide any additional context for the Bug.

The error causes the function to fail, but @dont_throw results in it just dropping the last attributes

👀 Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find similar issue

Are you willing to submit PR?

Yes I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions