From 2994640f475c8a65d671d970d48460715f345669 Mon Sep 17 00:00:00 2001 From: Paul Kompfner Date: Thu, 9 Oct 2025 16:45:47 -0400 Subject: [PATCH 1/2] Move `location` and `project_id` out of `InputParams` in `GoogleVertexLLMService`, making them top-level init args instead. We do this for two reasons: - Conceptually, these args comprise project-level setup, akin to credentials, whereas everything in `InputParams` is concerned with model configuration - Providing a `project_id` when initializing `GoogleVertexLLMService` should not be optional, but prior to the change in this commit it was (erroneously) treated as optional by dint of `InputParams` being optional This improvement was discussed [in this comment](https://github.com/pipecat-ai/pipecat/pull/2795#discussion_r2408279142). --- src/pipecat/services/google/llm_vertex.py | 67 ++++++++++++++++++++--- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/src/pipecat/services/google/llm_vertex.py b/src/pipecat/services/google/llm_vertex.py index ee66c1f20..3a3bf9ea9 100644 --- a/src/pipecat/services/google/llm_vertex.py +++ b/src/pipecat/services/google/llm_vertex.py @@ -51,6 +51,10 @@ class GoogleVertexLLMService(OpenAILLMService): class InputParams(OpenAILLMService.InputParams): """Input parameters specific to Vertex AI. + .. deprecated:: 0.0.90 + Use `OpenAILLMService.InputParams` instead and provide `location` and + `project_id` as direct arguments to `GoogleVertexLLMService.__init__()`. + Parameters: location: GCP region for Vertex AI endpoint (e.g., "us-east4"). project_id: Google Cloud project ID. @@ -60,12 +64,29 @@ class GoogleVertexLLMService(OpenAILLMService): location: str = "us-east4" project_id: str + def __init__(self, **kwargs): + """Initializes the InputParams.""" + import warnings + + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + warnings.warn( + "GoogleVertexLLMService.InputParams is deprecated. " + "Please use OpenAILLMService.InputParams instead and provide " + "'location' and 'project_id' as direct arguments to __init__.", + DeprecationWarning, + stacklevel=2, + ) + super().__init__(**kwargs) + def __init__( self, *, credentials: Optional[str] = None, credentials_path: Optional[str] = None, model: str = "google/gemini-2.0-flash-001", + location: Optional[str] = None, + project_id: Optional[str] = None, params: Optional[InputParams] = None, **kwargs, ): @@ -75,11 +96,42 @@ class GoogleVertexLLMService(OpenAILLMService): credentials: JSON string of service account credentials. credentials_path: Path to the service account JSON file. model: Model identifier (e.g., "google/gemini-2.0-flash-001"). + location: GCP region for Vertex AI endpoint (e.g., "us-east4"). + project_id: Google Cloud project ID. params: Vertex AI input parameters including location and project. + + .. deprecated:: 0.0.90 + Use `OpenAILLMService.InputParams` instead and provide `location` + and `project_id` as direct arguments to `__init__()`. + **kwargs: Additional arguments passed to OpenAILLMService. """ - params = params or OpenAILLMService.InputParams() - base_url = self._get_base_url(params) + # Handle deprecated InputParams + if params is not None and isinstance(params, GoogleVertexLLMService.InputParams): + # Extract location and project_id from params if not provided directly + if project_id is None: + project_id = params.project_id + if location is None: + location = params.location + # Convert to base InputParams + params = OpenAILLMService.InputParams( + **params.model_dump(exclude={"location", "project_id"}, exclude_unset=True) + ) + else: + params = params or OpenAILLMService.InputParams() + + # Validate parameters + # NOTE: once we remove deprecated InputParams, we can update __init__() + # signature with the following: + # - location: str = "us-east4", + # - project_id: str, + # For now, we need them as-is to maintain backward compatibility. + if project_id is None: + raise ValueError("project_id is required") + if location is None: + location = "us-east4" # Default location if not provided + + base_url = self._get_base_url(location, project_id) self._api_key = self._get_api_token(credentials, credentials_path) super().__init__( @@ -91,17 +143,14 @@ class GoogleVertexLLMService(OpenAILLMService): ) @staticmethod - def _get_base_url(params: InputParams) -> str: + def _get_base_url(location: str, project_id: str) -> str: """Construct the base URL for Vertex AI API.""" # Determine the correct API host based on location - if params.location == "global": + if location == "global": api_host = "aiplatform.googleapis.com" else: - api_host = f"{params.location}-aiplatform.googleapis.com" - return ( - f"https://{api_host}/v1/" - f"projects/{params.project_id}/locations/{params.location}/endpoints/openapi" - ) + api_host = f"{location}-aiplatform.googleapis.com" + return f"https://{api_host}/v1/projects/{project_id}/locations/{location}/endpoints/openapi" @staticmethod def _get_api_token(credentials: Optional[str], credentials_path: Optional[str]) -> str: From 8b62a96878da6863148ed923b2dcf1cb9d246940 Mon Sep 17 00:00:00 2001 From: Paul Kompfner Date: Fri, 10 Oct 2025 10:12:00 -0400 Subject: [PATCH 2/2] Improve how we're deprecating `location` and `project_id` in `GoogleVertexLLMService`, allowing user code to (correctly) continue referring to `GoogleVertexLLMService.InputParams`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also fix the slightly wrong (but so far harmless) pattern of initializing `OpenAILLMService.InputParams()` in the `GoogleVertexLLMService` if `params` wasn't provided—we should be letting the superclass decide what to do if the argument isn't specified. --- src/pipecat/services/google/llm_vertex.py | 69 +++++++++++++---------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/src/pipecat/services/google/llm_vertex.py b/src/pipecat/services/google/llm_vertex.py index 3a3bf9ea9..cc0b8654e 100644 --- a/src/pipecat/services/google/llm_vertex.py +++ b/src/pipecat/services/google/llm_vertex.py @@ -51,32 +51,45 @@ class GoogleVertexLLMService(OpenAILLMService): class InputParams(OpenAILLMService.InputParams): """Input parameters specific to Vertex AI. - .. deprecated:: 0.0.90 - Use `OpenAILLMService.InputParams` instead and provide `location` and - `project_id` as direct arguments to `GoogleVertexLLMService.__init__()`. - Parameters: location: GCP region for Vertex AI endpoint (e.g., "us-east4"). + + .. deprecated:: 0.0.90 + Use `location` as a direct argument to + `GoogleVertexLLMService.__init__()` instead. + project_id: Google Cloud project ID. + + .. deprecated:: 0.0.90 + Use `project_id` as a direct argument to + `GoogleVertexLLMService.__init__()` instead. """ # https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations - location: str = "us-east4" - project_id: str + location: Optional[str] = None + project_id: Optional[str] = None def __init__(self, **kwargs): """Initializes the InputParams.""" import warnings with warnings.catch_warnings(): - warnings.simplefilter("ignore", DeprecationWarning) - warnings.warn( - "GoogleVertexLLMService.InputParams is deprecated. " - "Please use OpenAILLMService.InputParams instead and provide " - "'location' and 'project_id' as direct arguments to __init__.", - DeprecationWarning, - stacklevel=2, - ) + warnings.simplefilter("always") + if "location" in kwargs and kwargs["location"] is not None: + warnings.warn( + "GoogleVertexLLMService.InputParams.location is deprecated. " + "Please provide 'location' as a direct argument to GoogleVertexLLMService.__init__() instead.", + DeprecationWarning, + stacklevel=2, + ) + + if "project_id" in kwargs and kwargs["project_id"] is not None: + warnings.warn( + "GoogleVertexLLMService.InputParams.project_id is deprecated. " + "Please provide 'project_id' as a direct argument to GoogleVertexLLMService.__init__() instead.", + DeprecationWarning, + stacklevel=2, + ) super().__init__(**kwargs) def __init__( @@ -87,7 +100,6 @@ class GoogleVertexLLMService(OpenAILLMService): model: str = "google/gemini-2.0-flash-001", location: Optional[str] = None, project_id: Optional[str] = None, - params: Optional[InputParams] = None, **kwargs, ): """Initializes the VertexLLMService. @@ -98,17 +110,13 @@ class GoogleVertexLLMService(OpenAILLMService): model: Model identifier (e.g., "google/gemini-2.0-flash-001"). location: GCP region for Vertex AI endpoint (e.g., "us-east4"). project_id: Google Cloud project ID. - params: Vertex AI input parameters including location and project. - - .. deprecated:: 0.0.90 - Use `OpenAILLMService.InputParams` instead and provide `location` - and `project_id` as direct arguments to `__init__()`. - **kwargs: Additional arguments passed to OpenAILLMService. """ - # Handle deprecated InputParams - if params is not None and isinstance(params, GoogleVertexLLMService.InputParams): - # Extract location and project_id from params if not provided directly + # Handle deprecated InputParams fields + if "params" in kwargs and isinstance(kwargs["params"], GoogleVertexLLMService.InputParams): + params = kwargs["params"] + # Extract location and project_id from params if not provided + # directly, for backward compatibility if project_id is None: project_id = params.project_id if location is None: @@ -117,15 +125,15 @@ class GoogleVertexLLMService(OpenAILLMService): params = OpenAILLMService.InputParams( **params.model_dump(exclude={"location", "project_id"}, exclude_unset=True) ) - else: - params = params or OpenAILLMService.InputParams() + kwargs["params"] = params - # Validate parameters - # NOTE: once we remove deprecated InputParams, we can update __init__() - # signature with the following: + # Validate project_id and location parameters + # NOTE: once we remove Vertex-spcific InputParams class, we can update + # __init__() signature as follows: # - location: str = "us-east4", # - project_id: str, - # For now, we need them as-is to maintain backward compatibility. + # But for now, we need them as-is to maintain proper backward + # compatibility. if project_id is None: raise ValueError("project_id is required") if location is None: @@ -138,7 +146,6 @@ class GoogleVertexLLMService(OpenAILLMService): api_key=self._api_key, base_url=base_url, model=model, - params=params, **kwargs, )