Skip to content

[BUG]: ddtrace for litellm.completion fails when metadata=None #16495

@connesy

Description

@connesy

Tracer Version(s)

4.4.0

Python Version(s)

3.13.12

Pip Version(s)

26.0

Bug Report

When using ddtrace with litellm, a call to litellm.completions with metadata=None fails, even though the metadata parameter is documented in LiteLLM as "dict (optional)": https://docs.litellm.ai/docs/completion/input#litellm-specific-params

Calling litellm.completions without specifying metadata works fine.

The issue is in https://github.com/DataDog/dd-trace-py/blob/v4.4.0/ddtrace/contrib/internal/litellm/utils.py#L14-L17, where when kwargs["metadata"] is None, the function tries calling None.get(...) which fails.

Reproduction Code

No response

Error Logs

Traceback (most recent call last):
  File "/home/connesy/litellm-chat/.venv/lib/python3.13/site-packages/litellm/batch_completion/main.py", line 120, in batch_completion
    results.append(future.result())
                   ~~~~~~~~~~~~~^^
  File "/home/connesy/.local/share/uv/python/cpython-3.13.12-linux-x86_64-gnu/lib/python3.13/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ~~~~~~~~~~~~~~~~~^^
  File "/home/connesy/.local/share/uv/python/cpython-3.13.12-linux-x86_64-gnu/lib/python3.13/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/home/connesy/.local/share/uv/python/cpython-3.13.12-linux-x86_64-gnu/lib/python3.13/concurrent/futures/thread.py", line 59, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/connesy/litellm-chat/.venv/lib/python3.13/site-packages/ddtrace/contrib/internal/trace_utils.py", line 315, in wrapper
    return func(mod, pin, wrapped, instance, args, kwargs)
  File "/home/connesy/litellm-chat/.venv/lib/python3.13/site-packages/ddtrace/contrib/internal/litellm/patch.py", line 54, in traced_completion
    host = extract_host_tag(kwargs)
  File "/home/connesy/litellm-chat/.venv/lib/python3.13/site-packages/ddtrace/contrib/internal/litellm/utils.py", line 15, in extract_host_tag
    if "host" in kwargs.get("metadata", {}).get("headers", {}):
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get'

Libraries in Use

No response

Operating System

Linux 6.17.0-14-generic #14~24.04.1-Ubuntu

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions