Skip to content

Commit fa68e3b

Browse files
fix(anthropic/chat/transformation.py): return citations as a provider-specific-field
Resolves #7970
1 parent cda73c9 commit fa68e3b

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

litellm/llms/anthropic/chat/transformation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ def transform_response(
628628
)
629629
else:
630630
text_content = ""
631+
citations: List[Any] = []
631632
tool_calls: List[ChatCompletionToolCallChunk] = []
632633
for idx, content in enumerate(completion_response["content"]):
633634
if content["type"] == "text":
@@ -645,10 +646,14 @@ def transform_response(
645646
index=idx,
646647
)
647648
)
649+
## CITATIONS
650+
if content.get("citations", None) is not None:
651+
citations.append(content["citations"])
648652

649653
_message = litellm.Message(
650654
tool_calls=tool_calls,
651655
content=text_content or None,
656+
provider_specific_fields={"citations": citations},
652657
)
653658

654659
## HANDLE JSON MODE - anthropic returns single function call

tests/llm_translation/test_anthropic_completion.py

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,45 +1092,32 @@ def test_anthropic_citations_api():
10921092
from litellm.llms.custom_httpx.http_handler import HTTPHandler
10931093
import json
10941094

1095-
client = HTTPHandler()
1096-
1097-
with patch.object(client, "post") as mock_post:
1098-
try:
1099-
resp = completion(
1100-
model="claude-3-5-sonnet-20241022",
1101-
messages=[
1095+
resp = completion(
1096+
model="claude-3-5-sonnet-20241022",
1097+
messages=[
1098+
{
1099+
"role": "user",
1100+
"content": [
11021101
{
1103-
"role": "user",
1104-
"content": [
1105-
{
1106-
"type": "document",
1107-
"source": {
1108-
"type": "text",
1109-
"media_type": "text/plain",
1110-
"data": "The grass is green. The sky is blue.",
1111-
},
1112-
"title": "My Document",
1113-
"context": "This is a trustworthy document.",
1114-
"citations": {"enabled": True},
1115-
},
1116-
{
1117-
"type": "text",
1118-
"text": "What color is the grass and sky?",
1119-
},
1120-
],
1121-
}
1102+
"type": "document",
1103+
"source": {
1104+
"type": "text",
1105+
"media_type": "text/plain",
1106+
"data": "The grass is green. The sky is blue.",
1107+
},
1108+
"title": "My Document",
1109+
"context": "This is a trustworthy document.",
1110+
"citations": {"enabled": True},
1111+
},
1112+
{
1113+
"type": "text",
1114+
"text": "What color is the grass and sky?",
1115+
},
11221116
],
1123-
client=client,
1124-
)
1125-
1126-
print(resp)
1127-
except Exception as e:
1128-
print(e)
1129-
1130-
mock_post.assert_called_once()
1131-
1132-
print(mock_post.call_args.kwargs)
1117+
}
1118+
],
1119+
)
11331120

1134-
json_data = json.loads(mock_post.call_args.kwargs["data"])
1121+
citations = resp.choices[0].message.provider_specific_fields["citations"]
11351122

1136-
assert json_data["messages"][0]["content"][0]["citations"]["enabled"]
1123+
assert citations is not None

0 commit comments

Comments
 (0)