Skip to content

Commit e132ad6

Browse files
authored
Merge pull request #14164 from TomeHirata/citation-supported-text-3
Add supported text field to anthropic citation response
2 parents f4ecf3c + bb5127b commit e132ad6

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

litellm/llms/anthropic/chat/transformation.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,15 @@ def extract_response_content(self, completion_response: dict) -> Tuple[
804804
if content.get("citations") is not None:
805805
if citations is None:
806806
citations = []
807-
citations.append(content["citations"])
807+
citations.append(
808+
[
809+
{
810+
**citation,
811+
"supported_text": content.get("text", ""),
812+
}
813+
for citation in content["citations"]
814+
]
815+
)
808816
if thinking_blocks is not None:
809817
reasoning_content = ""
810818
for block in thinking_blocks:

tests/llm_translation/test_anthropic_completion.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ def test_anthropic_tool_streaming():
256256
for chunk in anthropic_chunk_list:
257257
parsed_chunk = response_iter.chunk_parser(chunk)
258258
if tool_use := parsed_chunk.get("tool_use"):
259-
260259
# We only increment when a new block starts
261260
if tool_use.get("id") is not None:
262261
correct_tool_index += 1
@@ -920,6 +919,14 @@ def test_anthropic_citations_api():
920919
citations = resp.choices[0].message.provider_specific_fields["citations"]
921920

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

924931

925932
def test_anthropic_citations_api_streaming():
@@ -955,11 +962,9 @@ def test_anthropic_citations_api_streaming():
955962
has_citations = False
956963
for chunk in resp:
957964
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
965+
if provider_specific_fields := chunk.choices[0].delta.provider_specific_fields:
966+
if "citation" in provider_specific_fields:
967+
has_citations = True
963968

964969
assert has_citations
965970

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,30 @@ def test_extract_response_content_with_citations():
183183
}
184184

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

188211

189212
def test_map_tool_helper():

0 commit comments

Comments
 (0)