Skip to content

Commit afa6871

Browse files
Merge pull request #14026 from TomeHirata/citation-supported-text
Add supported text field to anthropic citation response
2 parents 55186da + 11228e9 commit afa6871

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

litellm/llms/anthropic/chat/handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ def chunk_parser(self, chunk: dict) -> ModelResponseStream:
743743
)
744744

745745
text, tool_use = self._handle_json_mode_chunk(text=text, tool_use=tool_use)
746+
if type_chunk:
747+
provider_specific_fields["chunk_type"] = type_chunk
746748

747749
returned_chunk = ModelResponseStream(
748750
choices=[

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

0 commit comments

Comments
 (0)