Skip to content

Commit 019b9fc

Browse files
committed
handle exceptions
1 parent 5a7b444 commit 019b9fc

3 files changed

Lines changed: 40 additions & 22 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ If you want to manage Workspaces, Data Sources and Pipes you might be looking fo
66

77
The SDK is meant to programatically ingest `NDJSON` data or send any request to an `API` instance.
88

9+
It contains handlers for:
10+
- logging events to a Tinybird Data Source from your Python module.
11+
- logging events from [litellm](https://www.litellm.ai/) to a Tinybird Data Source.
12+
913
## Ingest to a Tinybird DataSource
1014

1115
```python
@@ -108,6 +112,8 @@ pip install tinybird-python-sdk[ai]
108112
Then use the following handler:
109113

110114
```python
115+
import litellm
116+
from litellm import acompletion
111117
from tb.litellm.handler import TinybirdLitellmAsyncHandler
112118

113119
customHandler = TinybirdLitellmAsyncHandler(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "tinybird-python-sdk"
7-
version = "0.3.0"
7+
version = "0.3.1"
88
description = "Python SDK for Tinybird"
99
readme = "README.md"
1010
authors = [

tb/litellm/handler.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from datetime import datetime
1212
from tb.a.api import AsyncAPI as AsyncTinybird
1313
from tb.api import API as Tinybird
14+
import logging
1415

1516

1617
class CustomJSONEncoder(json.JSONEncoder):
@@ -90,33 +91,44 @@ def __init__(self, *args, **kwargs):
9091
super().__init__(*args, **kwargs)
9192

9293
def log_success_event(self, kwargs, response_obj, start_time, end_time):
93-
data = self._extract_data(kwargs, response_obj, start_time, end_time)
94-
self.api.send(
95-
f"events?name={self.datasource_name}",
96-
data=data
97-
)
98-
94+
try:
95+
data = self._extract_data(kwargs, response_obj, start_time, end_time)
96+
self.api.send(
97+
f"events?name={self.datasource_name}",
98+
data=data
99+
)
100+
except Exception as e:
101+
logging.error(f"Error logging success event: {e}")
99102
def log_failure_event(self, kwargs, response_obj, start_time, end_time):
100-
data = self._extract_data(kwargs, response_obj, start_time, end_time)
101-
self.api.send(
102-
f"events?name={self.datasource_name}",
103-
data=data
104-
)
103+
try:
104+
data = self._extract_data(kwargs, response_obj, start_time, end_time)
105+
self.api.send(
106+
f"events?name={self.datasource_name}",
107+
data=data
108+
)
109+
except Exception as e:
110+
logging.error(f"Error logging failure event: {e}")
105111

106112
class TinybirdLitellmAsyncHandler(TinybirdLitellmHandler):
107113
def __init__(self, *args, **kwargs):
108114
super().__init__(*args, **kwargs)
109115

110116
async def async_log_success_event(self, kwargs, response_obj, start_time, end_time):
111-
data = self._extract_data(kwargs, response_obj, start_time, end_time)
112-
await self.async_api.send(
113-
f"events?name={self.datasource_name}",
114-
data=data
115-
)
117+
try:
118+
data = self._extract_data(kwargs, response_obj, start_time, end_time)
119+
await self.async_api.send(
120+
f"events?name={self.datasource_name}",
121+
data=data
122+
)
123+
except Exception as e:
124+
logging.error(f"Error logging success event: {e}")
116125

117126
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time):
118-
data = self._extract_data(kwargs, response_obj, start_time, end_time)
119-
await self.async_api.send(
120-
f"events?name={self.datasource_name}",
121-
data=data
122-
)
127+
try:
128+
data = self._extract_data(kwargs, response_obj, start_time, end_time)
129+
await self.async_api.send(
130+
f"events?name={self.datasource_name}",
131+
data=data
132+
)
133+
except Exception as e:
134+
logging.error(f"Error logging failure event: {e}")

0 commit comments

Comments
 (0)