Skip to content

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]

fix(types): blank non-string datadog tool text fields, look the spend table up by name

mateo-berri1b568319d070
diff --git a/litellm/integrations/datadog/datadog_llm_obs.py b/litellm/integrations/datadog/datadog_llm_obs.py
index 3afe38ea075..98aac7336bf 100644
--- a/litellm/integrations/datadog/datadog_llm_obs.py
+++ b/litellm/integrations/datadog/datadog_llm_obs.py
@@ -254,13 +254,17 @@ def _reasoning_output_tokens(usage_object: Mapping[str, object] | None) -> float
)
-def _mapping_field(source: Mapping[str, object], key: str) -> Mapping[str, Any]:
+def _mapping_field(source: Mapping[str, object], key: str) -> Mapping[str, object]:
"""The value at `key` when it is a mapping, else an empty one."""
value: Final = source.get(key)
return value if isinstance(value, dict) else _EMPTY_MAPPING
-def _content_blocks(message: Mapping[str, object]) -> tuple[Mapping[str, Any], ...]:
+def _text_field(source: Mapping[str, object], key: str, default: str = "") -> str:
+ return _safe_identifier(source.get(key, default))
+
+
+def _content_blocks(message: Mapping[str, object]) -> tuple[Mapping[str, object], ...]:
content: Final = message.get("content")
if not isinstance(content, list):
return ()
@@ -293,10 +297,10 @@ def _to_dd_tool_calls(message: Mapping[str, object]) -> tuple[ToolCall, ...]:
raw_tool_calls: Final = message.get("tool_calls")
openai_calls: Final = tuple(
ToolCall(
- name=function.get("name", ""),
+ name=_text_field(function, "name"),
arguments=_to_dd_arguments(function.get("arguments", "")),
- tool_id=tool_call.get("id", ""),
- type=tool_call.get("type", "function"),
+ tool_id=_text_field(tool_call, "id"),
+ type=_text_field(tool_call, "type", "function"),
)
for tool_call in (raw_tool_calls if isinstance(raw_tool_calls, list) else ())
if isinstance(tool_call, dict)
@@ -304,9 +308,9 @@ def _to_dd_tool_calls(message: Mapping[str, object]) -> tuple[ToolCall, ...]:
)
anthropic_calls: Final = tuple(
ToolCall(
- name=block.get("name", ""),
+ name=_text_field(block, "name"),
arguments=_to_dd_arguments(block.get("input") or {}),
- tool_id=block.get("id", ""),
+ tool_id=_text_field(block, "id"),
type="tool_use",
)
for block in _content_blocks(message)
@@ -402,12 +406,12 @@ def _to_dd_messages(messages: object) -> tuple[Message, ...]:
def _to_dd_tool_definition(entry: Mapping[str, object]) -> ToolDefinition | None:
function: Final = entry.get("function")
- declared: Final[Mapping[str, Any]] = function if isinstance(function, dict) else entry
- name: Final = declared.get("name")
+ declared: Final[Mapping[str, object]] = function if isinstance(function, dict) else entry
+ name: Final = _text_field(declared, "name")
if not name:
return None
schema: Final = declared.get("parameters") or declared.get("input_schema")
- description: Final = declared.get("description", "")
+ description: Final = _text_field(declared, "description")
if not isinstance(schema, dict):
return ToolDefinition(name=name, description=description)
return ToolDefinition(name=name, description=description, schema=schema)
diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.py
index 865a8c39f03..5381071172f 100644
--- a/litellm/proxy/db/db_spend_update_writer.py
+++ b/litellm/proxy/db/db_spend_update_writer.py
@@ -19,7 +19,7 @@ from typing import TYPE_CHECKING, Any, Final, Literal, Protocol, TypeAlias, Type
from urllib.parse import quote, unquote
from pydantic import TypeAdapter
-from typing_extensions import LiteralString, ReadOnly, TypedDict, assert_never
+from typing_extensions import LiteralString, ReadOnly, TypedDict
import litellm
from litellm._logging import verbose_proxy_logger
@@ -142,17 +142,15 @@ _EntitySpendTable: TypeAlias = Literal[
def _entity_spend_table(batcher: _SpendBatch, table_accessor: _EntitySpendTable) -> BatchTable:
- match table_accessor:
- case "litellm_tagtable":
- return batcher.litellm_tagtable
- case "litellm_agentstable":
- return batcher.litellm_agentstable
- case "litellm_modelaccessgroupbudgettable":
- return batcher.litellm_modelaccessgroupbudgettable
- case "litellm_projecttable":
- return batcher.litellm_projecttable
- case _ as unreachable:
- assert_never(unreachable)
+ tables: Final[Mapping[_EntitySpendTable, BatchTable]] = MappingProxyType(
+ {
+ "litellm_tagtable": batcher.litellm_tagtable,
+ "litellm_agentstable": batcher.litellm_agentstable,
+ "litellm_modelaccessgroupbudgettable": batcher.litellm_modelaccessgroupbudgettable,
+ "litellm_projecttable": batcher.litellm_projecttable,
+ }
+ )
+ return tables[table_accessor]
class _SpendBatchManager(Protocol):
diff --git a/tests/test_litellm/integrations/datadog/test_datadog_llm_obs.py b/tests/test_litellm/integrations/datadog/test_datadog_llm_obs.py
index a2f81091893..77d2518696c 100644
--- a/tests/test_litellm/integrations/datadog/test_datadog_llm_obs.py
+++ b/tests/test_litellm/integrations/datadog/test_datadog_llm_obs.py
@@ -110,6 +110,23 @@ def test_output_tool_calls_use_the_datadog_tool_call_schema(logger: DataDogLLMOb
assert "function" not in message["tool_calls"][0]
+def test_tool_call_identifiers_that_are_not_strings_are_blanked_not_stringified(logger: DataDogLLMObsLogger) -> None:
+ payload = build(
+ logger,
+ response_message={
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [
+ {"id": {"nested": "call_1"}, "type": 7, "function": {"name": ["get_weather"], "arguments": "{}"}}
+ ],
+ },
+ )
+
+ assert payload["meta"]["output"]["messages"][0]["tool_calls"] == [
+ {"name": "", "arguments": {}, "tool_id": "", "type": ""}
+ ]
+
+
def test_tool_calls_are_not_duplicated_into_metadata(logger: DataDogLLMObsLogger) -> None:
"""The flat `output_tool_calls.*` keys were a second copy of a fact that now has its own field."""
payload = build(