Skip to content

Commit 3f9ab84

Browse files
committed
Reapply "Add supported text field to anthropic citation response"
1 parent 5ab3d74 commit 3f9ab84

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

litellm/llms/anthropic/chat/transformation.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,15 @@ def extract_response_content(self, completion_response: dict) -> Tuple[
797797
if content.get("citations") is not None:
798798
if citations is None:
799799
citations = []
800-
citations.append(content["citations"])
800+
citations.append(
801+
[
802+
{
803+
**citation,
804+
"supported_text": content.get("text", ""),
805+
}
806+
for citation in content["citations"]
807+
]
808+
)
801809
if thinking_blocks is not None:
802810
reasoning_content = ""
803811
for block in thinking_blocks:

tests/llm_translation/test_anthropic_completion.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,14 @@ def test_anthropic_citations_api():
920920
citations = resp.choices[0].message.provider_specific_fields["citations"]
921921

922922
assert citations is not None
923+
if citations:
924+
citation = citations[0][0]
925+
assert "supported_text" in citation
926+
assert "cited_text" in citation
927+
assert "document_index" in citation
928+
assert "document_title" in citation
929+
assert "start_char_index" in citation
930+
assert "end_char_index" in citation
923931

924932

925933
def test_anthropic_citations_api_streaming():
@@ -955,11 +963,11 @@ def test_anthropic_citations_api_streaming():
955963
has_citations = False
956964
for chunk in resp:
957965
print(f"returned chunk: {chunk}")
958-
if (
959-
chunk.choices[0].delta.provider_specific_fields
960-
and "citation" in chunk.choices[0].delta.provider_specific_fields
961-
):
962-
has_citations = True
966+
if provider_specific_fields := chunk.choices[0].delta.provider_specific_fields:
967+
if "citation" in provider_specific_fields:
968+
has_citations = True
969+
970+
assert "chunk_type" in provider_specific_fields
963971

964972
assert has_citations
965973

tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,23 @@ def test_calculate_usage_nulls(usage_object, expected_usage):
115115
assert hasattr(usage, k)
116116
assert getattr(usage, k) == v
117117

118-
@pytest.mark.parametrize("usage_object", [
119-
{
120-
"server_tool_use": {
121-
"web_search_requests": None
122-
}
123-
},
124-
{
125-
"server_tool_use": None
126-
}
127-
])
118+
119+
@pytest.mark.parametrize(
120+
"usage_object",
121+
[{"server_tool_use": {"web_search_requests": None}}, {"server_tool_use": None}],
122+
)
128123
def test_calculate_usage_server_tool_null(usage_object):
129124
"""
130125
Correctly deal with null values in usage object
131126
132127
Fixes https://github.com/BerriAI/litellm/issues/11920
133128
"""
134129
config = AnthropicConfig()
135-
130+
136131
usage = config.calculate_usage(usage_object=usage_object, reasoning_content=None)
137132
assert not hasattr(usage, "server_tool_use")
138133

134+
139135
def test_extract_response_content_with_citations():
140136
config = AnthropicConfig()
141137

@@ -188,7 +184,30 @@ def test_extract_response_content_with_citations():
188184
}
189185

190186
_, citations, _, _, _ = config.extract_response_content(completion_response)
191-
assert citations is not None
187+
assert citations == [
188+
[
189+
{
190+
"type": "char_location",
191+
"cited_text": "The grass is green. ",
192+
"document_index": 0,
193+
"document_title": "My Document",
194+
"start_char_index": 0,
195+
"end_char_index": 20,
196+
"supported_text": "the grass is green",
197+
},
198+
],
199+
[
200+
{
201+
"type": "char_location",
202+
"cited_text": "The sky is blue.",
203+
"document_index": 0,
204+
"document_title": "My Document",
205+
"start_char_index": 20,
206+
"end_char_index": 36,
207+
"supported_text": "the sky is blue",
208+
},
209+
],
210+
]
192211

193212

194213
def test_map_tool_helper():

0 commit comments

Comments
 (0)