-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: logging status code and error on the sdk #26
base: master
Are you sure you want to change the base?
Conversation
WalkthroughThe pull request introduces enhancements to the logging and error handling capabilities across several modules in the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (4)
- athina_logger/athina_meta.py (2 hunks)
- athina_logger/inference_logger.py (3 hunks)
- athina_logger/openai_wrapper.py (5 hunks)
- athina_logger/request_helper.py (1 hunks)
Additional context used
Ruff
athina_logger/request_helper.py
26-26: Local variable
error_message
is assigned to but never usedRemove assignment to unused variable
error_message
(F841)
27-27: Local variable
details_message
is assigned to but never usedRemove assignment to unused variable
details_message
(F841)
athina_logger/inference_logger.py
45-46: SyntaxError: Expected an indented block after
except
clause
Additional comments not posted (15)
athina_logger/athina_meta.py (2)
17-17
: LGTM!The addition of the
custom_eval_metrics
attribute with the correct type hint looks good.
18-18
: LGTM!The addition of the
status_code
attribute with the correct type hint looks good.athina_logger/request_helper.py (2)
20-23
: LGTM!The added print statements improve logging capabilities by providing visibility into the response data from the OpenAI API. This can be helpful for debugging purposes.
32-33
: Verify the impact of the change in error handling behavior.The change in error handling for
requests.exceptions.RequestException
alters the control flow and how errors are communicated to the caller. ReturningNone
instead of raising the exception could lead to unexpected behavior in the calling code if it is not updated to handle the new behavior.Consider reverting the change to maintain the expected behavior of raising the exception:
except requests.exceptions.RequestException as e: - print(f"Request exception: {e}") - return None + raise eAlternatively, ensure that the calling code is updated to handle the new behavior of returning
None
in case of arequests.exceptions.RequestException
.To verify the impact of this change, run the following script:
athina_logger/inference_logger.py (6)
39-40
: LGTM!The addition of
status_code
anderror
parameters to thelog_inference
function aligns with the PR objective and allows capturing more details about the inference request. The parameter types are correctly annotated.
43-43
: LGTM!The
args
tuple is correctly updated to include the newstatus_code
anderror
parameters, which are then passed to the_log_inference_asynchronously
method.
49-49
: LGTM!The
_log_inference_asynchronously
method signature is correctly updated to include the newstatus_code
anderror
parameters, which are then used in the method body to include additional details in the payload.
79-80
: LGTM!The payload dictionary is correctly updated to include the new
status_code
and a serialized version of theerror
using the newly added_serialize_error
method. This allows for more detailed logging of inference events, capturing the status code and any associated error information.
84-91
: LGTM!The inclusion of
traceback.print_exc()
in the error handling of the_log_inference_asynchronously
method provides more detailed error output in case of failures during the logging operation. This change enhances the debugging experience by providing more context about the errors.
93-117
: LGTM!The new
_serialize_error
method handles various types of error inputs, including dictionaries, strings, and exceptions, to extract and format the error message appropriately. It provides a consistent way to serialize errors for logging.The method correctly handles different scenarios:
- For dictionary inputs, it attempts to extract the inner error message by parsing the string as a dictionary.
- For string and exception inputs, it returns the error message as is.
This change enhances the error handling in the logging process and ensures that the error information is captured in a standardized format.
athina_logger/openai_wrapper.py (5)
Line range hint
33-56
: LGTM!The changes to include
status_code
anderror
in the logging payload are implemented correctly. This ensures that error details are consistently captured and logged to Athina.
100-106
: LGTM!The exception handling and error logging in the
wrapper
method are implemented correctly. This ensures that any exceptions during the OpenAI API call are captured and logged to Athina before re-raising the exception.
138-145
: LGTM!The exception handling and error logging in the
_response_interceptor
method for streaming responses are implemented correctly. This ensures that any exceptions during the processing of streaming responses are captured and logged to Athina before re-raising the exception.
149-162
: LGTM!The exception handling and error logging in the
_response_interceptor
method for non-streaming responses are implemented correctly. This ensures that any exceptions during the logging of non-streaming responses are captured and logged to Athina before re-raising the exception.
163-190
: LGTM!The
_log_exception_to_athina
method is implemented correctly. It centralizes the error logging logic and ensures consistent error reporting to Athina. The method constructs an error dictionary, updates theathina_meta
object with the error details, and logs the error using thelog_to_athina
function.
custom_eval_metrics: Optional[Dict] = None | ||
status_code: Optional[int] = None | ||
error: Optional[Any] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using a more specific type hint for the error
attribute.
Using Any
as the type hint for the error
attribute is very broad and could lead to inconsistencies in the stored error data. This could make it difficult to handle and process the stored error data consistently.
Consider using a more specific type hint, such as a custom Error
class or a Union
of specific error types, to ensure consistency in the stored error data.
error_message = response_json.get('error', 'Unknown Error') | ||
details_message = response_json.get( | ||
'details', {}).get('message', 'No Details') | ||
raise CustomException( | ||
response.status_code, f'{error_message}: {details_message}') | ||
response.status_code, f'{response_json}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the error handling behavior and remove unused variables.
The changes in this code segment have the following issues:
- The expected behavior of raising a
CustomException
with a detailed error message has been changed to include the entire response JSON. This could affect how the calling code handles failures. - The local variables
error_message
anddetails_message
are assigned but never used due to the changes in error handling.
Consider applying the following fixes:
- error_message = response_json.get('error', 'Unknown Error')
- details_message = response_json.get(
- 'details', {}).get('message', 'No Details')
raise CustomException(
response.status_code, f'{response_json}')
If the new error handling behavior is intended, ensure that the calling code is updated to handle the change. Otherwise, consider reverting the change to maintain the expected behavior of raising a CustomException
with a detailed error message:
error_message = response_json.get('error', 'Unknown Error')
details_message = response_json.get(
'details', {}).get('message', 'No Details')
- raise CustomException(
- response.status_code, f'{response_json}')
+ raise CustomException(
+ response.status_code, f'{error_message}: {details_message}')
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
error_message = response_json.get('error', 'Unknown Error') | |
details_message = response_json.get( | |
'details', {}).get('message', 'No Details') | |
raise CustomException( | |
response.status_code, f'{error_message}: {details_message}') | |
response.status_code, f'{response_json}') | |
raise CustomException( | |
response.status_code, f'{response_json}') |
Tools
Ruff
26-26: Local variable
error_message
is assigned to but never usedRemove assignment to unused variable
error_message
(F841)
27-27: Local variable
details_message
is assigned to but never usedRemove assignment to unused variable
details_message
(F841)
print(f"Unexpected error: {e}") | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip
Codebase Verification
Potential issues identified with the error handling change
The change in error handling for make_post_request
function could lead to unexpected behavior in some parts of the codebase. Here are the specific concerns:
-
In
athina_logger/log_stream_inference/openai_chat_completion_stream.py
andathina_logger/log_stream_inference/openai_completion_stream.py
, exceptions are currently re-raised. ReturningNone
instead of raising an exception could lead to aTypeError
if the code assumes a response object. -
In
athina_logger/tracing/trace.py
, themake_post_request
function is called without a try-except block. ReturningNone
could lead to unexpected behavior if the code assumes a response object.
To address these issues:
- Consider reverting the change in
make_post_request
to maintain the expected behavior of raising exceptions. - If keeping the new behavior, update the calling code in the mentioned files to handle the possibility of receiving
None
as a return value.
Analysis chain
Verify the impact of the change in error handling behavior.
Similar to the previous comment, the change in error handling for unexpected exceptions alters the control flow and how errors are communicated to the caller. Returning None
instead of raising the exception could lead to unexpected behavior in the calling code if it is not updated to handle the new behavior.
Consider reverting the change to maintain the expected behavior of raising the exception:
except Exception as e:
- print(f"Unexpected error: {e}")
- return None
+ raise e
Alternatively, ensure that the calling code is updated to handle the new behavior of returning None
in case of an unexpected exception.
To verify the impact of this change, run the following script:
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify the usage of the `make_post_request` function.
# Test: Search for the function usage. Expect: Calling code handles the new behavior of returning `None`.
rg --type python -A 5 $'make_post_request'
Length of output: 72
Script:
#!/bin/bash
# Description: Find Python files and search for make_post_request function usage
# Find all Python files
python_files=$(fd -e py)
# Search for make_post_request in Python files
for file in $python_files; do
echo "Searching in $file:"
grep -n "make_post_request" "$file" -A 5
echo "---"
done
Length of output: 8357
Summary by CodeRabbit
New Features
Bug Fixes
Chores