langfuse-python icon indicating copy to clipboard operation
langfuse-python copied to clipboard

check if response has valid json before logging it.

Open gkmngrgn opened this issue 10 months ago • 1 comments

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_response by adding parse_json_response function in langfuse/request.py.

  • Behavior:
    • Adds parse_json_response function in _process_response to handle JSON parsing and logging.
    • Raises APIError for invalid JSON responses in _process_response.
    • Logs response text for status codes 200 and 201 if return_json is False.
  • Error Handling:
    • Improves error handling for non-JSON responses by raising APIError with status code and message.
  • Logging:
    • Logs response text in _process_response for successful status codes and when JSON parsing is attempted.

This description was created by Ellipsis for e2e0e887048446cacd8883723f97b39a43eb880b. It will automatically update as commits are pushed.

gkmngrgn avatar Jan 27 '25 00:01 gkmngrgn

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jan 27 '25 00:01 CLAassistant

Thanks again for raising this, this will be part of our upcoming SDK major release 👍🏾

hassiebp avatar Jul 15 '25 08:07 hassiebp