The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(databricks): add anthropic citation support
TomuHirata7333060fb046
diff --git a/docs/my-website/docs/providers/databricks.md b/docs/my-website/docs/providers/databricks.mdindex 8631cbfdad9..921b06a17b7 100644--- a/docs/my-website/docs/providers/databricks.md+++ b/docs/my-website/docs/providers/databricks.md@@ -282,6 +282,11 @@ ModelResponse()```+### Citations++Anthropic models served through Databricks can return citation metadata. LiteLLM+exposes these via `response.choices[0].message.provider_specific_fields["citations"]`.+### Pass `thinking` to Anthropic modelsYou can also pass the `thinking` parameter to Anthropic models.diff --git a/litellm/llms/databricks/chat/transformation.py b/litellm/llms/databricks/chat/transformation.pyindex 908419f7193..5600d5c6426 100644--- a/litellm/llms/databricks/chat/transformation.py+++ b/litellm/llms/databricks/chat/transformation.py@@ -379,6 +379,21 @@ class DatabricksConfig(DatabricksBase, OpenAILikeChatConfig, AnthropicConfig):thinking_blocks.append(thinking_block)return reasoning_content, thinking_blocks+ @staticmethod+ def extract_citations(+ content: Optional[AllDatabricksContentValues],+ ) -> Optional[List[Any]]:+ if content is None:+ return None+ citations: Optional[List[Any]] = None+ if isinstance(content, list):+ for item in content:+ if item.get("citations") is not None:+ if citations is None:+ citations = []+ citations.append(item["citations"])+ return citations+def _transform_dbrx_choices(self, choices: List[DatabricksChoice], json_mode: Optional[bool] = None) -> List[Choices]:@@ -427,12 +442,19 @@ class DatabricksConfig(DatabricksBase, OpenAILikeChatConfig, AnthropicConfig):choice["message"].get("content"))+ citations = DatabricksConfig.extract_citations(+ choice["message"].get("content")+ )+translated_message = Message(role="assistant",content=content_str,reasoning_content=reasoning_content,thinking_blocks=thinking_blocks,tool_calls=choice["message"].get("tool_calls"),+ provider_specific_fields={"citations": citations}+ if citations is not None+ else None,)if finish_reason is None:@@ -561,6 +583,12 @@ class DatabricksChatResponseIterator(BaseModelResponseIterator):for _tc in tool_calls:if _tc.get("function", {}).get("arguments") == "{}":_tc["function"]["arguments"] = "" # avoid invalid json+ citation = choice["delta"].get("citation")+ if citation is not None:+ choice["delta"].setdefault("provider_specific_fields", {})[+ "citation"+ ] = citation+ choice["delta"].pop("citation", None)# extract the content strcontent_str = DatabricksConfig.extract_content_str(choice["delta"].get("content")diff --git a/litellm/types/llms/databricks.py b/litellm/types/llms/databricks.pyindex bb59b692ef7..37151408161 100644--- a/litellm/types/llms/databricks.py+++ b/litellm/types/llms/databricks.py@@ -1,5 +1,5 @@import json-from typing import Any, List, Literal, Optional, TypedDict, Union+from typing import Any, Dict, List, Literal, Optional, TypedDict, Unionfrom pydantic import BaseModelfrom typing_extensions import (@@ -24,9 +24,10 @@ class GenericStreamingChunk(TypedDict, total=False):usage: Optional[BaseModel]-class DatabricksTextContent(TypedDict):+class DatabricksTextContent(TypedDict, total=False):type: Literal["text"]text: Required[str]+ citations: Optional[List[Dict[str, Any]]]class DatabricksReasoningSummary(TypedDict):diff --git a/tests/test_litellm/llms/databricks/chat/test_databricks_chat_transformation.py b/tests/test_litellm/llms/databricks/chat/test_databricks_chat_transformation.pyindex fc44d44aba9..d61f826e89b 100644--- a/tests/test_litellm/llms/databricks/chat/test_databricks_chat_transformation.py+++ b/tests/test_litellm/llms/databricks/chat/test_databricks_chat_transformation.py@@ -10,7 +10,10 @@ sys.path.insert() # Adds the parent directory to the system pathfrom unittest.mock import MagicMock, patch-from litellm.llms.databricks.chat.transformation import DatabricksConfig+from litellm.llms.databricks.chat.transformation import (+ DatabricksChatResponseIterator,+ DatabricksConfig,+)def test_transform_choices():@@ -90,3 +93,51 @@ def test_transform_choices_without_signature():thinking_block = choices[0].message.thinking_blocks[0]assert thinking_block["type"] == "thinking"assert thinking_block["thinking"] == "i'm thinking without signature."+++def test_transform_choices_with_citations():+ config = DatabricksConfig()+ databricks_choices = [+ {+ "message": {+ "role": "assistant",+ "content": [+ {+ "type": "text",+ "text": "Paris",+ "citations": [{"source": "wiki"}],+ }+ ],+ },+ "index": 0,+ "finish_reason": "stop",+ }+ ]++ choices = config._transform_dbrx_choices(choices=databricks_choices)++ assert choices[0].message.provider_specific_fields == {+ "citations": [[{"source": "wiki"}]]+ }+++def test_chunk_parser_with_citation():+ iterator = DatabricksChatResponseIterator(None, sync_stream=True)+ chunk = {+ "id": "1",+ "object": "chat.completion.chunk",+ "created": 0,+ "model": "test",+ "choices": [+ {+ "delta": {"citation": {"source": "wiki"}},+ "index": 0,+ "finish_reason": None,+ }+ ],+ }++ parsed = iterator.chunk_parser(chunk)+ assert parsed.choices[0].delta.provider_specific_fields == {+ "citation": {"source": "wiki"}+ }