Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Optimize has_more_than_n_keys function and BatchTraceProcessor #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/agents/strict_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,4 @@ def is_list(obj: object) -> TypeGuard[list[object]]:


def has_more_than_n_keys(obj: dict[str, object], n: int) -> bool:
i = 0
for _ in obj.keys():
i += 1
if i > n:
return True
return False
return len(obj) > n
15 changes: 7 additions & 8 deletions src/agents/tracing/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,22 @@ def _export_batches(self, force: bool = False):
"""
while True:
items_to_export: list[Span[Any] | Trace] = []

# Gather a batch of spans up to max_batch_size
while not self._queue.empty() and (
force or len(items_to_export) < self._max_batch_size
):
max_items = self._max_batch_size if not force else float('inf')
# Try to get up to max_items from the queue without checking empty() repeatedly
for _ in range(int(max_items)):
try:
items_to_export.append(self._queue.get_nowait())
except queue.Empty:
# Another thread might have emptied the queue between checks
# Queue is empty, no need to continue the loop
break

# If we collected nothing, we're done
if not items_to_export:
break

# Export the batch
self._exporter.export(items_to_export)
# If not forcing and queue is likely empty, we can stop
if not force and len(items_to_export) < max_items:
break


# Create a shared global instance:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_has_more_than_n_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from agents.strict_schema import has_more_than_n_keys


def test_has_more_than_n_keys():
# Test with empty dict
assert has_more_than_n_keys({}, 0) is False
# Test with dict having exactly n keys
assert has_more_than_n_keys({"a": 1}, 1) is False
# Test with dict having more than n keys
assert has_more_than_n_keys({"a": 1, "b": 2}, 1) is True
# Test with large dict
large_dict = {str(i): object() for i in range(1000)}
assert has_more_than_n_keys(large_dict, 500) is True
assert has_more_than_n_keys(large_dict, 1000) is False