Skip to content

Commit

Permalink
bug fix for upload() just run as upload() no asyncio
Browse files Browse the repository at this point in the history
  • Loading branch information
DoKu88 committed Nov 15, 2024
1 parent 03167c8 commit f170020
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "synth-sdk"
version = "0.2.68"
version = "0.2.69"
description = ""
authors = [{name = "Synth AI", email = "[email protected]"}]
license = {text = "MIT"}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="synth-sdk",
version="0.2.68",
version="0.2.69",
packages=find_packages(),
install_requires=[
"opentelemetry-api",
Expand Down
17 changes: 7 additions & 10 deletions synth_sdk/tracing/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,17 @@ def upload(dataset: Dataset, traces: List[SystemTrace]=[], verbose: bool = False
async def upload_wrapper(dataset, traces, verbose, show_payload):
result = await upload_helper(dataset, traces, verbose, show_payload)
return result


# If we're in an async context (event loop is running)
if is_event_loop_running():
logging.info("Event loop is already running")
task = asyncio.create_task(upload_wrapper(dataset, traces, verbose, show_payload))
# Wait for the task if called from an async function
if asyncio.current_task():
return task # Returning the task to be awaited if in async context
else:
# Run task synchronously by waiting for it to finish if in sync context
return asyncio.get_event_loop().run_until_complete(task)

# Return the coroutine directly for async contexts
return upload_helper(dataset, traces, verbose, show_payload)
else:
# In sync context, run the coroutine and return the result
logging.info("Event loop is not running")
return asyncio.run(upload_wrapper(dataset, traces, verbose, show_payload))
return asyncio.run(upload_helper(dataset, traces, verbose, show_payload))


async def upload_helper(dataset: Dataset, traces: List[SystemTrace]=[], verbose: bool = False, show_payload: bool = False):
"""Upload all system traces and dataset to the server."""
Expand Down
2 changes: 1 addition & 1 deletion testing/ai_agent_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def run_test():
# Upload traces
try:
logger.info("Attempting to upload traces")
response, _ = await upload(dataset=dataset, verbose=True)
response, payload, dataset, traces = await upload(dataset=dataset, verbose=True)
logger.info("Upload successful!")
print("Upload successful!")
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion testing/ai_agent_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def run_test():
# Upload traces
try:
logger.info("Attempting to upload traces")
response, _ = await upload(dataset=dataset, verbose=True)
response, payload, dataset, traces= await upload(dataset=dataset, verbose=True)
logger.info("Upload successful!")
print("Upload successful!")
except Exception as e:
Expand Down
4 changes: 2 additions & 2 deletions testing/upload_payload_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async def test_upload_async(mock_send_system_traces):
# Upload traces
logger.info("Attempting to upload traces, async version")
# TODO: uploads traces directly from list of traces (override the event_store)
response, payload = await upload(dataset=dataset, verbose=True, show_payload=True)
response, payload, dataset, traces = await upload(dataset=dataset, verbose=True, show_payload=True)
logger.info("Upload successful!")

# Pytest assertion
Expand All @@ -174,7 +174,7 @@ def test_upload_sync(mock_send_system_traces):
# Upload traces
logger.info("Attempting to upload traces, non-async version")
# TODO: uploads traces directly from list of traces (override the event_store)
response, payload = upload(dataset=dataset, verbose=True, show_payload=True)
response, payload, dataset, traces = upload(dataset=dataset, verbose=True, show_payload=True)
logger.info("Upload successful!")

# Pytest assertion
Expand Down

0 comments on commit f170020

Please sign in to comment.