anthropic[patch]: always return content blocks if citations are generated (#29398)

We currently return string (and therefore no content blocks / citations)
if the response is of the form
```
[
    {"text": "a claim", "citations": [...]},
]
```

There are other cases where we do return citations as-is:
```
[
    {"text": "a claim", "citations": [...]},
    {"text": "some other text"},
    {"text": "another claim", "citations": [...]},
]
```
Here we update to return content blocks including citations in the first
case as well.
This commit is contained in:
ccurme
2025-01-23 18:47:23 -05:00
committed by GitHub
parent 933b35b9c5
commit ed797e17fb
2 changed files with 32 additions and 1 deletions

View File

@@ -764,7 +764,11 @@ class ChatAnthropic(BaseChatModel):
llm_output = {
k: v for k, v in data_dict.items() if k not in ("content", "role", "type")
}
if len(content) == 1 and content[0]["type"] == "text":
if (
len(content) == 1
and content[0]["type"] == "text"
and "citations" not in content[0]
):
msg = AIMessage(content=content[0]["text"])
elif any(block["type"] == "tool_use" for block in content):
tool_calls = extract_tool_calls(content)