From 6aefe76351623141ce6b128721d41d288631b170 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 30 Oct 2025 14:09:57 +0000 Subject: [PATCH] Optimize AsyncFiles.delete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization achieves a 7% runtime improvement by streamlining parameter handling in the `make_request_options` function. **Key optimizations:** 1. **Eliminated unnecessary dictionary operations**: The original code always performed `{**options.get("params", {}), **extra_query}` which requires a dictionary lookup, potential empty dict creation, and merging operations even when `query` is None. The optimized version uses conditional logic to avoid these operations: - If both `query` and `extra_query` exist: merge with `{**query, **extra_query}` - If only one exists: assign directly without merging - This eliminates the `options.get("params", {})` call and unnecessary dict operations 2. **Replaced function call with direct type checking**: Changed `is_given(post_parser)` to `not isinstance(post_parser, NotGiven) and not isinstance(post_parser, Omit)`. The line profiler shows this check consuming 22.7% of execution time in the original version. Direct `isinstance` checks are faster than function calls, especially in hot paths. **Performance impact**: The line profiler shows the optimized version reduces total execution time from 631.49μs to 559.61μs in `make_request_options`. The parameter handling optimization particularly benefits scenarios where only one of `query` or `extra_query` is provided (common in API calls), avoiding unnecessary dictionary operations. The direct type checking provides consistent speedup across all invocations by eliminating function call overhead. These optimizations are most effective for high-frequency API request scenarios where `make_request_options` is called repeatedly, as demonstrated in the test cases with concurrent executions and varied options. --- src/openai/_base_client.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/openai/_base_client.py b/src/openai/_base_client.py index 58490e4430..7f0a8cc083 100644 --- a/src/openai/_base_client.py +++ b/src/openai/_base_client.py @@ -1861,11 +1861,12 @@ def make_request_options( if extra_body is not None: options["extra_json"] = cast(AnyMapping, extra_body) - if query is not None: + if query is not None and extra_query is not None: + options["params"] = {**query, **extra_query} + elif query is not None: options["params"] = query - - if extra_query is not None: - options["params"] = {**options.get("params", {}), **extra_query} + elif extra_query is not None: + options["params"] = extra_query if not isinstance(timeout, NotGiven): options["timeout"] = timeout @@ -1873,7 +1874,7 @@ def make_request_options( if idempotency_key is not None: options["idempotency_key"] = idempotency_key - if is_given(post_parser): + if not isinstance(post_parser, NotGiven) and not isinstance(post_parser, Omit): # internal options["post_parser"] = post_parser # type: ignore