-
Notifications
You must be signed in to change notification settings - Fork 1
fix: Fix Duplicate Trace Calls for Async Functions and Optimize Tracer Implementation #473
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…nput extraction and logging finalization - Introduced `_extract_function_inputs` to streamline input extraction for logging. - Added `_finalize_step_logging` to encapsulate step timing and logging logic. - Implemented `_handle_trace_completion` for improved trace completion handling. - Enhanced `trace_async` decorator to support both async functions and async generators with optimized logging. - Refactored existing tracing logic to utilize new helper functions for better maintainability and readability.
- Refactored client initialization logic into a new `_get_client` function for better lazy loading. - Ensured the Openlayer client is only created when needed, improving resource management. - Updated data streaming calls to utilize the new client retrieval method, enhancing code readability and maintainability.
… and maintainability - Introduced `_create_and_initialize_step` to encapsulate step creation and parent-child relationships. - Enhanced `_handle_trace_completion` to streamline trace completion and data streaming logic. - Refactored existing tracing functions to utilize new helper methods, improving code readability. - Added `_log_step_exception` and `_process_wrapper_inputs_and_outputs` for better error handling and input/output processing. - Updated async generator handling to ensure proper tracing during iteration.
…ions - Replaced `_create_step_for_async_generator` with a direct call to `_create_and_initialize_step` to streamline async step creation. - Updated the parameters for step initialization to enhance clarity and maintainability. - Improved overall code readability by reducing function complexity.
gustavocidornelas
approved these changes
Jul 9, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Eliminate Duplicate Trace Calls in Async Functions and Optimize Tracer Implementation
Problem Statement
The Openlayer Python SDK was experiencing duplicate trace calls when using the
@trace()
decorator on async functions. Instead of generating a single unified trace, async functions created multiple duplicate traces with different data, causing test failures and incorrect tracing behavior.Root Cause Analysis
The issue was traced to the
@trace_async()
decorator's incompatibility with async generator functions:with create_step()
) that interfered withyield
statementscoroutine
objects instead ofasync_generator
objectsSolution Implementation
1. Async Generator Detection and Handling
inspect.isasyncgenfunction(func)
at decoration time to identify async generatorsyield
statements remain at the top level of wrapper functions2. Code Optimization
Identified and eliminated massive code duplication across three wrapper functions:
3. Client Authentication Fix
_client
was initialized at import time before environment variables were set_get_client()
function4. Test Refactoring
Created comprehensive nested test scenario with single orchestrator method:
intelligent_assistant_main()
method with 7 nested operations