-
Notifications
You must be signed in to change notification settings - Fork 116
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
check if response has valid json before logging it. #1087
base: main
Are you sure you want to change the base?
Conversation
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.
Disclaimer: Experimental PR review
PR Summary
This PR adds improved JSON response handling and validation in the Langfuse Python client.
- Added
parse_json_response
helper function inlangfuse/request.py
to safely validate and parse JSON responses before logging - Improved error handling by catching invalid JSON responses that could previously cause unhandled exceptions
- Optimized logging by only attempting JSON parsing when needed (status codes 200/201)
- Consolidated duplicate JSON parsing logic into a single reusable function for better maintainability
💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!
1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile
raise APIError(response.status_code, "Invalid JSON response received") | ||
|
||
log.debug("received response: %s", response.text) | ||
return json_response |
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.
@gkmngrgn thanks for raising this PR! If the parsing fails, we would fall back to None. I would prefer to return response.text
to have the error reason propagated to the ApiError construction further below
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.
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.
@hassiebp hi again, any update that I can help you in this PR?
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.
Thanks again for raising this!
That's a suggestion for -> langfuse/langfuse#4464
Honestly, I'm not sure about if we can consider it as a bug. Because the response comes from outside and there's no guarantee that
res.text
is always json serializable.There were many options to make the outputs more human-readable but I preferred this way:
Important
Improves JSON parsing and error handling in
_process_response
by addingparse_json_response
function inlangfuse/request.py
.parse_json_response
function in_process_response
to handle JSON parsing and logging.APIError
for invalid JSON responses in_process_response
.return_json
is False.APIError
with status code and message._process_response
for successful status codes and when JSON parsing is attempted.This description was created by
for e2e0e88. It will automatically update as commits are pushed.