langchain/libs/partners
ccurme 73c76b9628
anthropic[patch]: always add tool_result type to ToolMessage content (#22721)
Anthropic tool results can contain image data, which are typically
represented with content blocks having `"type": "image"`. Currently,
these content blocks are passed as-is as human/user messages to
Anthropic, which raises BadRequestError as it expects a tool_result
block to follow a tool_use.

Here we update ChatAnthropic to nest the content blocks inside a
tool_result content block.

Example:
```python
import base64

import httpx
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from langchain_core.pydantic_v1 import BaseModel, Field


# Fetch image
image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")


class FetchImage(BaseModel):
    should_fetch: bool = Field(..., description="Whether an image is requested.")


llm = ChatAnthropic(model="claude-3-sonnet-20240229").bind_tools([FetchImage])

messages = [
    HumanMessage(content="Could you summon a beautiful image please?"),
    AIMessage(
        content=[
            {
                "type": "tool_use",
                "id": "toolu_01Rn6Qvj5m7955x9m9Pfxbcx",
                "name": "FetchImage",
                "input": {"should_fetch": True},
            },
        ],
        tool_calls=[
            {
                "name": "FetchImage",
                "args": {"should_fetch": True},
                "id": "toolu_01Rn6Qvj5m7955x9m9Pfxbcx",
            },
        ],
    ),
    ToolMessage(
        name="FetchImage",
        content=[
            {
                "type": "image",
                "source": {
                    "type": "base64",
                    "media_type": "image/jpeg",
                    "data": image_data,
                },
            },
        ],
        tool_call_id="toolu_01Rn6Qvj5m7955x9m9Pfxbcx",
    ),
]

llm.invoke(messages)
```

Trace:
https://smith.langchain.com/public/d27e4fc1-a96d-41e1-9f52-54f5004122db/r
2024-06-13 20:14:23 -07:00
..
ai21 multiple: implement ls_params (#22621) 2024-06-06 16:51:37 +00:00
airbyte airbyte[patch]: airbyte-cdk compatible pydantic versions (#21738) 2024-05-15 19:13:25 -07:00
anthropic anthropic[patch]: always add tool_result type to ToolMessage content (#22721) 2024-06-13 20:14:23 -07:00
azure-dynamic-sessions docs: add aca-ds (#21746) 2024-05-16 08:52:07 +00:00
chroma partners: fix numpy dep (#22858) 2024-06-13 14:46:42 -04:00
couchbase couchbase: Add the initial version of Couchbase partner package (#22087) 2024-06-07 14:04:08 -07:00
exa multiple: releases with relaxed core dep (#21724) 2024-05-15 19:29:35 +00:00
fireworks multiple: implement ls_params (#22621) 2024-06-06 16:51:37 +00:00
groq docs: Standardize ChatGroq (#22751) 2024-06-14 03:08:36 +00:00
huggingface docs: standardize ChatHuggingFace (#22693) 2024-06-10 20:54:36 +00:00
ibm ibm[patch]: release 0.1.7 (#21737) 2024-05-15 19:10:15 -07:00
milvus milvus[patch]: Release 0.1.1 (#22499) 2024-06-04 12:00:53 -07:00
mistralai mistral[patch]: add usage_metadata to (a)invoke and (a)stream (#22781) 2024-06-11 15:34:50 -04:00
mongodb partners: fix numpy dep (#22858) 2024-06-13 14:46:42 -04:00
nomic nomic[patch]: Release 0.1.2 (#22561) 2024-06-05 17:06:58 +00:00
openai partners: fix numpy dep (#22858) 2024-06-13 14:46:42 -04:00
pinecone partners: fix numpy dep (#22858) 2024-06-13 14:46:42 -04:00
prompty docs: fix some spelling mistakes caught by newest version of code spell (#22090) 2024-05-23 16:59:11 -04:00
qdrant qdrant[patch]: Use collection_exists API instead of exceptions (#22764) 2024-06-13 20:01:32 -07:00
robocorp robocorp: typo (#22509) 2024-06-04 15:33:38 -04:00
together together: bump langchain-core (#22616) 2024-06-06 14:09:40 +00:00
upstage together, upstage: bump minimum langchain-openai version (#22505) 2024-06-04 15:20:41 -04:00
voyageai partners: Remove unnecessary print from voyageai embeddings (#21865) 2024-05-18 08:57:17 -04:00