Skip to content

Commit c94d11b

Browse files
authored
logfire.instrument_litellm() docs (#1415)
1 parent d48f1ea commit c94d11b

File tree

8 files changed

+83
-11
lines changed

8 files changed

+83
-11
lines changed

docs/guides/web-ui/llm-panels.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Click an LLM span to open the details panel.
4747
| [OpenAI](../../integrations/llms/openai.md) ||||
4848
| [Google Gen AI](../../integrations/llms/google-genai.md) ||||
4949
| [LangChain](../../integrations/llms/langchain.md) ||||
50+
| [LiteLLM](../../integrations/llms/litellm.md) ||||
5051
| [Anthropic](../../integrations/llms/anthropic.md) | | ||
5152
| [Google ADK](https://github.com/pydantic/logfire/issues/1201#issuecomment-3012423974) || | |
5253

153 KB
Loading

docs/integrations/llms/google-genai.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ integration: logfire
33
---
44
# Google Gen AI SDK
55

6-
**Logfire** supports instrumenting calls to the [Google Gen AI SDK (`google-genai`)](https://googleapis.github.io/python-genai/) with the [`logfire.instrument_google_genai()`][logfire.Logfire.instrument_google_genai] method, for example:
6+
**Logfire** supports instrumenting calls to the [Google Gen AI SDK (`google-genai`)](https://googleapis.github.io/python-genai/) with the [`logfire.instrument_google_genai()`][logfire.Logfire.instrument_google_genai] method.
77

8-
```python
8+
## Installation
9+
10+
Install `logfire` with the `google-genai` extra:
11+
12+
{{ install_logfire(extras=['google-genai']) }}
13+
14+
## Usage
15+
16+
```python hl_lines="8 11"
917
import os
1018

1119
from google.genai import Client

docs/integrations/llms/litellm.md

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
---
2-
integration: third-party
2+
integration: logfire
33
---
4+
# LiteLLM
45

5-
LiteLLM allows you to call over 100 Large Language Models (LLMs) using the same input/output format. It also supports Logfire for logging and monitoring.
6+
**Logfire** supports instrumenting calls to the [LiteLLM](https://docs.litellm.ai/) Python SDK with the [`logfire.instrument_litellm()`][logfire.Logfire.instrument_litellm] method.
67

7-
To integrate Logfire with LiteLLM:
8+
## Installation
89

9-
1. Set the `LOGFIRE_TOKEN` environment variable.
10-
2. Add `logfire` to the callbacks of LiteLLM.
10+
Install `logfire` with the `litellm` extra:
1111

12-
For more details, [check the official LiteLLM documentation.](https://docs.litellm.ai/docs/observability/logfire_integration)
12+
{{ install_logfire(extras=['litellm']) }}
13+
14+
## Usage
15+
16+
```python hl_lines="6"
17+
import litellm
18+
19+
import logfire
20+
21+
logfire.configure()
22+
logfire.instrument_litellm()
23+
24+
response = litellm.completion(
25+
model='gpt-4o-mini',
26+
messages=[{'role': 'user', 'content': 'Hi'}],
27+
)
28+
print(response.choices[0].message.content)
29+
# > Hello! How can I assist you today?
30+
```
31+
32+
!!! warning
33+
This currently works best if all arguments of instrumented methods are passed as keyword arguments,
34+
e.g. `litellm.completion(model=model, messages=messages)`.
35+
36+
This creates a span which shows the conversation in the Logfire UI:
37+
38+
<figure markdown="span">
39+
![Logfire LiteLLM conversation](../../images/logfire-screenshot-litellm-llm-panel.png){ width="697" }
40+
</figure>
41+
42+
[`logfire.instrument_litellm()`][logfire.Logfire.instrument_litellm] uses the `LiteLLMInstrumentor().instrument()` method of the [`openinference-instrumentation-litellm`](https://pypi.org/project/openinference-instrumentation-litellm/) package.
43+
44+
!!! note
45+
[LiteLLM has its own integration with Logfire](https://docs.litellm.ai/docs/observability/logfire_integration), but we recommend using `logfire.instrument_litellm()` instead.

logfire/_internal/integrations/litellm.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
from typing import Any
22

3-
from openinference.instrumentation.litellm import LiteLLMInstrumentor
4-
53
import logfire
64

5+
try:
6+
from openinference.instrumentation.litellm import LiteLLMInstrumentor
7+
except ImportError:
8+
raise RuntimeError(
9+
'The `logfire.instrument_litellm()` method '
10+
'requires the `openinference-instrumentation-litellm` package.\n'
11+
'You can install this with:\n'
12+
" pip install 'logfire[litellm]'"
13+
)
14+
715

816
def instrument_litellm(logfire_instance: logfire.Logfire, **kwargs: Any):
917
LiteLLMInstrumentor().instrument(

logfire/_internal/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,16 @@ def instrument_google_genai(self, **kwargs: Any):
13011301
instrument_google_genai(self, **kwargs)
13021302

13031303
def instrument_litellm(self, **kwargs: Any):
1304+
"""Instrument the [LiteLLM](https://docs.litellm.ai/) Python SDK.
1305+
1306+
!!! warning
1307+
This currently works best if all arguments of instrumented methods are passed as keyword arguments,
1308+
e.g. `litellm.completion(model=model, messages=messages)`.
1309+
1310+
Uses the `LiteLLMInstrumentor().instrument()` method of the
1311+
[`openinference-instrumentation-litellm`](https://pypi.org/project/openinference-instrumentation-litellm/)
1312+
package, to which it passes `**kwargs`.
1313+
"""
13041314
from .integrations.litellm import instrument_litellm
13051315

13061316
self._warn_if_not_initialized_for_instrumentation()

mkdocs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ nav:
117117
- Google Gen AI: integrations/llms/google-genai.md
118118
- Anthropic: integrations/llms/anthropic.md
119119
- LangChain: integrations/llms/langchain.md
120+
- LiteLLM: integrations/llms/litellm.md
120121
- MCP: integrations/llms/mcp.md
121122
- LLamaIndex: integrations/llms/llamaindex.md
122123
- Mirascope: integrations/llms/mirascope.md
123-
- LiteLLM: integrations/llms/litellm.md
124124
- Magentic: integrations/llms/magentic.md
125125
- Web Frameworks:
126126
- Web Frameworks: integrations/web-frameworks/index.md

tests/otel_integrations/test_litellm.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import warnings
44
from typing import Any
5+
from unittest import mock
56

67
import pydantic
78
import pytest
@@ -14,6 +15,17 @@
1415
from logfire.testing import TestExporter
1516

1617

18+
def test_missing_opentelemetry_dependency() -> None:
19+
with mock.patch.dict('sys.modules', {'openinference.instrumentation.litellm': None}):
20+
with pytest.raises(RuntimeError) as exc_info:
21+
logfire.instrument_litellm()
22+
assert str(exc_info.value) == snapshot("""\
23+
The `logfire.instrument_litellm()` method requires the `openinference-instrumentation-litellm` package.
24+
You can install this with:
25+
pip install 'logfire[litellm]'\
26+
""")
27+
28+
1729
@pytest.mark.vcr()
1830
@pytest.mark.skipif(get_version(pydantic.__version__) < get_version('2.5.0'), reason='Requires newer pydantic version')
1931
def test_litellm_instrumentation(exporter: TestExporter) -> None:

0 commit comments

Comments
 (0)