{"record":{"id":"e815591a9543fe3c","repo":"sgl-project/sglang","slug":"generativemodel","errorCode":null,"errorMessage":"GenerativeModel","messagePattern":"GenerativeModel","errorType":"error_code","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"python/sglang/lang/backend/vertexai.py","lineNumber":25,"sourceCode":"from sglang.lang.ir import SglSamplingParams\n\ntry:\n    import vertexai\n    from vertexai.preview.generative_models import (\n        GenerationConfig,\n        GenerativeModel,\n        Image,\n    )\nexcept ImportError as e:\n    GenerativeModel = e\n\n\nclass VertexAI(BaseBackend):\n    def __init__(self, model_name, safety_settings=None):\n        super().__init__()\n\n        if isinstance(GenerativeModel, Exception):\n            raise GenerativeModel\n\n        project_id = os.environ[\"GCP_PROJECT_ID\"]\n        location = os.environ.get(\"GCP_LOCATION\")\n        vertexai.init(project=project_id, location=location)\n\n        self.model_name = model_name\n        self.chat_template = get_chat_template(\"default\")\n        self.safety_settings = safety_settings\n\n    def get_chat_template(self):\n        return self.chat_template\n\n    def generate(\n        self,\n        s: StreamExecutor,\n        sampling_params: SglSamplingParams,\n    ):\n        if s.messages_:","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/lang/backend/vertexai.py#L7-L43","documentation":"In the VertexAI backend's __init__, a module-level import of GenerativeModel was wrapped in try/except that stores the exception; if the import failed (e.g. google-cloud-aiplatform not installed), __init__ re-raises that stored import error. It indicates the optional VertexAI dependency is missing or broken.","triggerScenarios":"Instantiating VertexAI backend (e.g. lang backend selection for Gemini models) without google-cloud-aiplatform / vertexai installed, or with an incompatible version where `from vertexai.generative_models import GenerativeModel` fails.","commonSituations":"Using sglang's lang frontend with backend=\"vertexai\" in an environment lacking the extra dependency; dependency conflicts after upgrading google libraries; CI environments that only install core requirements.","solutions":["pip install google-cloud-aiplatform (and shirley-vertexai or the vertexai package version the code expects)","Check the stored exception: run `import vertexai.generative_models` in the same interpreter to see the real import error","Pin compatible versions of google-auth / google-cloud-aiplatform if there is a dependency conflict"],"exampleFix":"# before\nbackend = VertexAI(\"gemini-1.5-pro\")\n# after\npip install google-cloud-aiplatform\nbackend = VertexAI(\"gemini-1.5-pro\")","handlingStrategy":"validation","validationCode":"try:\n    import vertexai  # noqa\n    from vertexai.generative_models import GenerativeModel  # noqa\n    vertexai_ok = True\nexcept Exception:\n    vertexai_ok = False\nif not vertexai_ok:\n    raise SystemExit(\"Install google-cloud-aiplatform to use the VertexAI backend\")","typeGuard":"def has_vertexai() -> bool:\n    try:\n        import vertexai.generative_models  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    backend = VertexAI(model_name)\nexcept Exception as e:\n    raise RuntimeError(f\"VertexAI backend unavailable: {e}\") from e","preventionTips":["Install optional extras in the same environment as sglang when using non-default backends","Smoke-test the backend import in deployment startup checks","Pin google-cloud-aiplatform versions to avoid transitive breakage"],"tags":["import-error","optional-dependency","vertexai","environment"],"backgroundTag":"missing-optional-dependency","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}