langfuse-python
langfuse-python copied to clipboard
check if response has valid json before logging it.
That's a suggestion for -> https://github.com/langfuse/langfuse/issues/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:
- Added a sub function to serialize and log the response at the same time, in each if statement.
- If the status code is 200 or 201, we may not need to the json output, so it logs response text manually, for the specific case.
[!IMPORTANT] Improves JSON parsing and error handling in
_process_responseby addingparse_json_responsefunction inlangfuse/request.py.
- Behavior:
- Adds
parse_json_responsefunction in_process_responseto handle JSON parsing and logging.- Raises
APIErrorfor invalid JSON responses in_process_response.- Logs response text for status codes 200 and 201 if
return_jsonis False.- Error Handling:
- Improves error handling for non-JSON responses by raising
APIErrorwith status code and message.- Logging:
- Logs response text in
_process_responsefor successful status codes and when JSON parsing is attempted.This description was created by
for e2e0e887048446cacd8883723f97b39a43eb880b. It will automatically update as commits are pushed.
Thanks again for raising this, this will be part of our upcoming SDK major release 👍🏾