Skip to content

Commit 7919d4f

Browse files
itogastonIñigo Gastonmichaelnchin
authored
Add citation support #537 (#542)
Add citation support. [Here](https://python.langchain.com/docs/integrations/chat/anthropic/#simple-example) is an example using langchain_anthropic. ```py from langchain_core.messages import HumanMessage, SystemMessage from langchain_aws import ChatBedrockConverse llm = ChatBedrockConverse( model="eu.anthropic.claude-3-7-sonnet-20250219-v1:0", max_tokens=1000, temperature=0.1, ) response = llm.invoke( input=[ HumanMessage( content=[ { "type": "document", "document": { "format": "txt", "source": { "text": "The grass is green. The sky is blue." }, "name": "My Document", 'context': 'This is a trustworthy document.', "citations": {"enabled": True}, }, }, {"type": "text", "text": "What color is the grass and sky?"}, ] ), ], ) response.content ``` This results in a Validation ``` ValueError: Unexpected content block type in content. Expected to have one of 'text', 'tool_use', 'image', 'video, 'document', 'tool_result','json', 'guard_content', 'reasoning_content', or 'citations_content' keys. Received: {'citations_content': {'content': [{'text': 'The grass is green.'}], 'citations': [{'title': 'My Document', 'source_content': [{'text': 'The grass is green. '}], 'location': {'document_char': {'document_index': 0, 'start': 0, 'end': 20}}}]}} ``` with the incoming changes the `citations` list is preserved as I believe langchain_anthropic [does](https://github.com/langchain-ai/langchain/blob/ad44f0688b1cc0809e526b9bf1ff68127aee8d48/libs/partners/anthropic/langchain_anthropic/chat_models.py#L2150) --------- Co-authored-by: Iñigo Gaston <[email protected]> Co-authored-by: Michael Chin <[email protected]>
1 parent d97e1b3 commit 7919d4f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

libs/aws/langchain_aws/chat_models/bedrock_converse.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,13 +1378,25 @@ def _bedrock_to_lc(content: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
13781378
},
13791379
}
13801380
)
1381+
elif "citations_content" in block:
1382+
citations_dict = block.get("citations_content", {})
1383+
content_items = citations_dict.get("content", [])
1384+
citations = citations_dict.get("citations", [])
1385+
1386+
for content_item in content_items:
1387+
if "text" in content_item:
1388+
text_block = {"type": "text", "text": content_item["text"]}
1389+
if citations:
1390+
# Preserve original Bedrock citations format
1391+
text_block["citations"] = citations
1392+
lc_content.append(text_block)
13811393

13821394
else:
13831395
raise ValueError(
13841396
"Unexpected content block type in content. Expected to have one of "
13851397
"'text', 'tool_use', 'image', 'video, 'document', 'tool_result',"
1386-
"'json', 'guard_content', or "
1387-
"'reasoning_content' keys. Received:\n\n{block}"
1398+
"'json', 'guard_content', 'citations_content' or "
1399+
f"'reasoning_content' keys. Received:\n\n{block}"
13881400
)
13891401
return lc_content
13901402

0 commit comments

Comments
 (0)