From c9eb585b1185d00b5a457087f17f68dbc450cd81 Mon Sep 17 00:00:00 2001 From: Anas Khan <83116240+anxkhn@users.noreply.github.com> Date: Mon, 6 Jul 2026 15:34:49 +0530 Subject: [PATCH] fix(complete): type top_p as float in Completion.create Completion.create and AsyncCompletion.create annotated top_p as Union[bool, NotGiven], but top_p is a nucleus-sampling probability (a float). Every other top_p in the SDK and the underlying OpenAI completions parameter is a float, and the sibling sampling params (temperature, frequency_penalty, presence_penalty) in the same two signatures are already Union[float, NotGiven]. The bool annotation made a valid call such as completions.create(..., top_p=0.9) fail type checkers and led IDEs to suggest True/False for a probability field. Change the annotation to Union[float, NotGiven] at both sites. Annotation-only; no runtime behavior changes. Add an offline regression test asserting the top_p annotation includes float (and not bool) for both the sync and async create signatures. Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com> --- portkey_ai/api_resources/apis/complete.py | 4 ++-- tests/test_complete_top_p_annotation.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/test_complete_top_p_annotation.py diff --git a/portkey_ai/api_resources/apis/complete.py b/portkey_ai/api_resources/apis/complete.py index 24fa0ef1..80792796 100644 --- a/portkey_ai/api_resources/apis/complete.py +++ b/portkey_ai/api_resources/apis/complete.py @@ -134,7 +134,7 @@ def create( stream: Union[bool, NotGiven] = NOT_GIVEN, temperature: Union[float, NotGiven] = NOT_GIVEN, max_tokens: Union[int, NotGiven] = NOT_GIVEN, - top_p: Union[bool, NotGiven] = NOT_GIVEN, + top_p: Union[float, NotGiven] = NOT_GIVEN, best_of: Union[int, NotGiven] = NOT_GIVEN, echo: Union[bool, NotGiven] = NOT_GIVEN, frequency_penalty: Union[float, NotGiven] = NOT_GIVEN, @@ -317,7 +317,7 @@ async def create( stream: Union[bool, NotGiven] = NOT_GIVEN, temperature: Union[float, NotGiven] = NOT_GIVEN, max_tokens: Union[int, NotGiven] = NOT_GIVEN, - top_p: Union[bool, NotGiven] = NOT_GIVEN, + top_p: Union[float, NotGiven] = NOT_GIVEN, best_of: Union[int, NotGiven] = NOT_GIVEN, echo: Union[bool, NotGiven] = NOT_GIVEN, frequency_penalty: Union[float, NotGiven] = NOT_GIVEN, diff --git a/tests/test_complete_top_p_annotation.py b/tests/test_complete_top_p_annotation.py new file mode 100644 index 00000000..4979d747 --- /dev/null +++ b/tests/test_complete_top_p_annotation.py @@ -0,0 +1,20 @@ +import inspect +import typing + +from portkey_ai import AsyncCompletion, Completion + + +def test_completion_top_p_is_typed_as_float(): + annotation = inspect.signature(Completion.create).parameters["top_p"].annotation + args = typing.get_args(annotation) + assert float in args + assert bool not in args + + +def test_async_completion_top_p_is_typed_as_float(): + annotation = ( + inspect.signature(AsyncCompletion.create).parameters["top_p"].annotation + ) + args = typing.get_args(annotation) + assert float in args + assert bool not in args