googleapis / googleapis/python-genai
python """ اسکریپت: اتصال به Vertex AI و اجرای مدل DeepSeek (MaaS) نویسنده: [نام شما/محسن باقری] توضیحات: احراز هویت از طریق Service Account و فراخوانی مدلهای MaaS در پلتفرم Vertex AI گوگل """
- Dominant language
- Python
- Stars
- 4k
- Forks
- 1k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 40
Description
> I am confused, when i call the method to generate content with a service account authorization in place it works, but with the model list it doesn't
>
> Here is how I get my credentials:
> ```python
> def get_credentials(GOOGLE_API_KEY) -> gapi_key.Credentials:
> # credentials = gapi_key.Credentials(GOOGLE_API_KEY)
> scopes = [
> "https://www.googleapis.com/auth/generative-language",
> "https://www.googleapis.com/auth/cloud-platform",
> ]
> service_account = json.loads(service_account_file)
> credentials = gservice_account.Credentials.from_service_account_info(service_account, scopes=scopes)
>
> credentials = get_credentials("")
> credentials.refresh(google.auth.transport.requests.Request())
> print("token is ", credentials.token)
> print(f"https://{location}-aiplatform.googleapis.com")
>
>
> vertexai.init(credentials=credentials, project=project_id, api_endpoint=f"https://{location}-aiplatform.googleapis.com", location=location)
>
> client = genai.Client(vertexai=True, credentials=credentials)
>
> response = vertexai.generative_models.GenerativeModel("deepseek-ai/deepseek-r1-0528-maas").generate_content("What is 1 + 1")
> print("From vertex ai directly", response.candidates[0].content)
>
> print(client.models.list(config={'query_base':True}).page)
> ```
>
> When I call the list method it still returns an error
> ```
> token is
> https://us-central1-aiplatform.googleapis.com
> Traceback (most recent call last):
> File "/workspaces/pipelines/test.py", line 90, in
> client = genai.Client(vertexai=True, credentials=credentials)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/usr/local/python/3.12.1/lib/python3.12/site-packages/google/genai/client.py", line 219, in __init__
> self._api_client = self._get_api_client(
> ^^^^^^^^^^^^^^^^^^^^^
> File "/usr/local/python/3.12.1/lib/python3.12/site-packages/google/genai/client.py", line 265, in _get_api_client
> return BaseApiClient(
> ^^^^^^^^^^^^^^
> File "/usr/local/python/3.12.1/lib/python3.12/site-packages/google/genai/_api_client.py", line 537, in __init__
> credentials, self.project = _load_auth(project=None)
> ^^^^^^^^^^^^^^^^^^^^^^^^
> File "/usr/local/python/3.12.1/lib/python3.12/site-packages/google/genai/_api_client.py", line 184, in _load_auth
> credentials, loaded_project_id = google.auth.default( # type: ignore[no-untyped-call]
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/home/codespace/.local/lib/python3.12/site-packages/google/auth/_default.py", line 685, in default
> raise exceptions.DefaultCredentialsError(_CLOUD_SDK_MISSING_CREDENTIALS)
> google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.
> ```
_Originally posted by @lmtr0 in [#1126](https://github.com/googleapis/python-genai/issues/1126#issuecomment-3109248813)_
import json
import google.auth.transport.requests
from google.oauth2 import service_account as gservice_account
import vertexai
from google import genai
# --- تنظیمات ---
SERVICE_ACCOUNT_FILE = "path/to/service-account.json" # مسیر فایل JSON حساب سرویس
PROJECT_ID = "your-project-id"
LOCATION = "us-central1" # یا هر region که مدل MaaS در آن فعال است
SCOPES = [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/generative-language",
]
def get_credentials(service_account_file: str) -> gservice_account.Credentials:
"""ساخت Credentials از فایل حساب سرویس."""
with open(service_account_file, "r", encoding="utf-8") as f:
service_account_info = json.load(f)
credentials = gservice_account.Credentials.from_service_account_info(
service_account_info, scopes=SCOPES
)
return credentials # ← این return جا افتاده بود
credentials = get_credentials(SERVICE_ACCOUNT_FILE)
credentials.refresh(google.auth.transport.requests.Request())
print("token is", credentials.token)
API_ENDPOINT = f"https://{LOCATION}-aiplatform.googleapis.com"
print(API_ENDPOINT)
vertexai.init(
credentials=credentials,
project=PROJECT_ID,
api_endpoint=API_ENDPOINT,
location=LOCATION,
)
client = genai.Client(vertexai=True, credentials=credentials)
# مدل MaaS (DeepSeek) از طریق client.models صدا زده میشود
response = client.models.generate_content(
model="deepseek-ai/deepseek-r1-0528-maas",
contents="What is 1 + 1",
)
print("From Vertex AI directly:", response.candidates[0].content)
models_page = client.models.list(config={"query_base": True})
print(models_page)
Contributor guide
Research direction
Start with the failing client construction in google/genai/client.py, then read _load_auth in google/genai/_api_client.py and run the supplied script to reproduce the DefaultCredentialsError. Compare the credentials passed to genai.Client with the traceback path; done means the client initializes and client.models.list returns a result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- google-cloud, python
- Domain
- ai, api, cloud
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100