fix(sdk-python): avoid empty JSON body on GET#1606
fix(sdk-python): avoid empty JSON body on GET#1606mahendrarathore1742 wants to merge 2 commits intolangfuse:mainfrom
Conversation
Return None when no additional body parameters are provided so GET requests don't include {} payloads. Adds unit tests for request body handling.
|
Thanks for raising this! This should've been already fixed on main. Could you please share a small repro example for your case? |
|
Hi @hassiebp! Here's the repro context you asked for. This issue was also independently reported in the main langfuse repo: langfuse/langfuse#12939 Root cause (confirmed): Minimal repro: from langfuse import Langfuse
langfuse = Langfuse(
public_key="pk-...",
secret_key="sk-...",
host="https://your-self-hosted-endpoint"
)
# Passing request_options to any GET endpoint triggers the bug
trace = langfuse.api.trace.get(
trace_id, request_options={"timeout_in_seconds": 30}
)Affected version: Python SDK Observed error: Google Cloud load balancer rejects the request due to a GET request carrying a body ( Could you clarify which commit on |
This PR fixes an issue where the SDK would send an empty JSON body ({}) with GET requests, which violates HTTP specs and is rejected by some load balancers (e.g., Google Cloud). Now, if no additional body parameters are provided, the request body is set to None for GET requests.
Fix: Avoid sending {} as the body for GET requests.
Adds unit tests to ensure correct request body behavior.
Disclaimer: Experimental PR review
Greptile Summary
This PR fixes a bug where GET requests could send an empty JSON body (
{}) that is rejected by some load balancers (e.g., Google Cloud). The fix refactorsmaybe_filter_request_bodyto returnNonedirectly when noadditional_body_parametersare present, rather than returning{}and relying on a downstreamjson_body != {}guard inget_request_bodyto convert it toNone.Key changes:
maybe_filter_request_bodynow short-circuits toNonewhenrequest_optionsisNoneor whenadditional_body_parametersis absent/empty, making the intent explicit.!= {}safety check inget_request_body(lines 204–206) is retained and still useful for the non-Nonedatamapping path, which is unchanged.request_options=Nonewould complete coverage of the new early-return path.Confidence Score: 5/5
request_options=None). The core logic change is sound:maybe_filter_request_bodypreviously returned{}for the no-body case which was already neutralised by the!= {}check downstream, and the new code simply makes thatNonereturn explicit. No regressions are introduced.tests/test_http_client.py.Important Files Changed
maybe_filter_request_bodyto returnNonedirectly whenadditional_body_parametersis absent/empty, replacing the indirect{} → Noneconversion that relied on a downstream!= {}check inget_request_body. Logic is correct and cleaner.request_options=Nonetest for completeness, but all existing assertions are correct.Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[get_request_body called] --> B{data is not None?} B -- Yes --> C[maybe_filter_request_body\nwith data] B -- No --> D[maybe_filter_request_body\nwith json] D --> E{request_options\nis None?} E -- Yes --> F[return None\nNEW short-circuit] E -- No --> G{additional_body_parameters\nfalsy or missing?} G -- Yes --> H[return None\nNEW short-circuit] G -- No --> I[return jsonable_encoder\nof params] C --> J[data_content merged dict] I --> K[json_body set] F --> K H --> K J --> L[data_body set] K --> M{json_body is empty dict?} M -- Yes --> N[return None\nsafety net] M -- No --> O[return json_body] L --> P{data_body is empty dict?} P -- Yes --> Q[return None\nsafety net] P -- No --> R[return data_body] style F fill:#90EE90 style H fill:#90EE90Reviews (1): Last reviewed commit: "fix(sdk-python): avoid empty JSON body o..." | Re-trigger Greptile
(2/5) Greptile learns from your feedback when you react with thumbs up/down!