Skip to content

Commit e24a16b

Browse files
authored
fix(api): ensure we only send events of 1mb max (#228)
1 parent 38175ad commit e24a16b

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

langfuse/task_manager.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
from langfuse.request import LangfuseClient
1616
from langfuse.serializer import DatetimeSerializer
1717

18-
# largest message size in db is 331000 bytes right now
19-
MAX_MSG_SIZE = 700_000
18+
# largest message size in db is 331_000 bytes right now
19+
MAX_MSG_SIZE = 650_000
2020

2121
# https://vercel.com/docs/functions/serverless-functions/runtimes#request-body-size
2222
# The maximum payload size for the request body or the response body of a Serverless Function is 4.5 MB
2323
# 4_500_000 Bytes = 4.5 MB
24-
BATCH_SIZE_LIMIT = 2_000_000
24+
# https://nextjs.org/docs/pages/building-your-application/routing/api-routes#custom-config
25+
# The default nextjs body parser takes a max body size of 1mb. Hence, our BATCH_SIZE_LIMIT should be less to accomodate the final event.
26+
BATCH_SIZE_LIMIT = 650_000
2527

2628

2729
class Consumer(threading.Thread):
@@ -68,6 +70,9 @@ def _next(self):
6870
item = queue.get(block=True, timeout=self._flush_interval - elapsed)
6971
item_size = len(json.dumps(item, cls=DatetimeSerializer).encode())
7072
self._log.debug(f"item size {item_size}")
73+
if item_size > MAX_MSG_SIZE:
74+
self._log.warning("Item exceeds size limit (size: %s), dropping item. (%s)", item_size, item)
75+
continue
7176
items.append(item)
7277
total_size += item_size
7378
if total_size >= BATCH_SIZE_LIMIT:

0 commit comments

Comments
 (0)