From b9c344dba6ad6b9b3754554b38e65bbfe2fcacc1 Mon Sep 17 00:00:00 2001 From: Li Yin Date: Mon, 23 Jun 2025 17:28:29 -0700 Subject: [PATCH] Fix mutable default args in OpenAI client --- .../components/model_client/openai_client.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/adalflow/adalflow/components/model_client/openai_client.py b/adalflow/adalflow/components/model_client/openai_client.py index aeab3664e..cea421a87 100644 --- a/adalflow/adalflow/components/model_client/openai_client.py +++ b/adalflow/adalflow/components/model_client/openai_client.py @@ -402,7 +402,7 @@ def _convert_llm_inputs_to_messages( def convert_inputs_to_api_kwargs( self, input: Optional[Any] = None, - model_kwargs: Dict = {}, + model_kwargs: Optional[Dict] = None, model_type: ModelType = ModelType.UNDEFINED, ) -> Dict: r""" @@ -423,7 +423,7 @@ def convert_inputs_to_api_kwargs( Dict: API-specific kwargs for the model call """ - final_model_kwargs = model_kwargs.copy() + final_model_kwargs = model_kwargs.copy() if model_kwargs else {} if model_type == ModelType.EMBEDDER: if isinstance(input, str): input = [input] @@ -494,7 +494,11 @@ def parse_image_generation_response(self, response: List[Image]) -> GeneratorOut ), max_time=5, ) - def call(self, api_kwargs: Dict = {}, model_type: ModelType = ModelType.UNDEFINED): + def call( + self, + api_kwargs: Optional[Dict] = None, + model_type: ModelType = ModelType.UNDEFINED, + ): """ kwargs is the combined input and model_kwargs. Support streaming call. For reasoning model, users can add "reasoning" key to the api_kwargs to pass the reasoning config. @@ -507,6 +511,7 @@ def call(self, api_kwargs: Dict = {}, model_type: ModelType = ModelType.UNDEFINE } } """ + api_kwargs = api_kwargs or {} log.info(f"api_kwargs: {api_kwargs}") self._api_kwargs = api_kwargs if model_type == ModelType.EMBEDDER: @@ -559,12 +564,15 @@ def call(self, api_kwargs: Dict = {}, model_type: ModelType = ModelType.UNDEFINE max_time=5, ) async def acall( - self, api_kwargs: Dict = {}, model_type: ModelType = ModelType.UNDEFINED + self, + api_kwargs: Optional[Dict] = None, + model_type: ModelType = ModelType.UNDEFINED, ): """ kwargs is the combined input and model_kwargs """ # store the api kwargs in the client + api_kwargs = api_kwargs or {} self._api_kwargs = api_kwargs if self.async_client is None: self.async_client = self.init_async_client()