Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions litellm/llms/anthropic/chat/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ def chunk_parser(self, chunk: dict) -> ModelResponseStream:
)

text, tool_use = self._handle_json_mode_chunk(text=text, tool_use=tool_use)
if type_chunk:
provider_specific_fields["chunk_type"] = type_chunk

returned_chunk = ModelResponseStream(
choices=[
Expand Down
10 changes: 9 additions & 1 deletion litellm/llms/anthropic/chat/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,15 @@ def extract_response_content(self, completion_response: dict) -> Tuple[
if content.get("citations") is not None:
if citations is None:
citations = []
citations.append(content["citations"])
citations.append(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a unit test for this behaviour inside test_litellm/

[
{
**citation,
"supported_text": content.get("text", ""),
}
for citation in content["citations"]
]
)
if thinking_blocks is not None:
reasoning_content = ""
for block in thinking_blocks:
Expand Down
18 changes: 13 additions & 5 deletions tests/llm_translation/test_anthropic_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,14 @@ def test_anthropic_citations_api():
citations = resp.choices[0].message.provider_specific_fields["citations"]

assert citations is not None
if citations:
citation = citations[0][0]
assert "supported_text" in citation
assert "cited_text" in citation
assert "document_index" in citation
assert "document_title" in citation
assert "start_char_index" in citation
assert "end_char_index" in citation


def test_anthropic_citations_api_streaming():
Expand Down Expand Up @@ -955,11 +963,11 @@ def test_anthropic_citations_api_streaming():
has_citations = False
for chunk in resp:
print(f"returned chunk: {chunk}")
if (
chunk.choices[0].delta.provider_specific_fields
and "citation" in chunk.choices[0].delta.provider_specific_fields
):
has_citations = True
if provider_specific_fields := chunk.choices[0].delta.provider_specific_fields:
if "citation" in provider_specific_fields:
has_citations = True

assert "chunk_type" in provider_specific_fields

assert has_citations

Expand Down
Loading