-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(provider) add gemini provider #1507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xieyxclack
merged 20 commits into
agentscope-ai:main
from
ekzhu:claude/add-gemini-provider-3Gy4l
Mar 16, 2026
+571
−5
Merged
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dccc37c
feat: add Google Gemini as a built-in model provider
claude 11b4e95
feat: update Gemini models to latest and add Japanese/French i18n
claude 8c328c1
Add google-genai as optional dependency group for Gemini provider
claude bf0be40
Make google-genai a core dependency instead of optional
claude a9fd162
Bump google-genai minimum version to >=1.67.0
claude 8c0fc65
Fix formatting and lint issues for CI checks
claude c919e93
Remove French and Japanese locale additions to reduce PR scope
claude 8f7ffae
Remove leftover FR locale import from console i18n
claude 058c1b5
Revert unrelated console formatting changes to reduce PR scope
claude 947708d
Add unit tests for GeminiProvider
claude 60238fb
Reorder model normalization to strip prefix before display_name fallback
claude 8f36a25
Narrow exception handling from generic Exception to genai APIError
claude 94825f3
Update Gemini fallback model IDs to match current API names
claude cd58e66
Add GeminiChatModel to ChatModelName Literal type
claude c64be9d
Fix Gemini provider hanging on save: add missing await and timeout
claude 236f7a9
Address PR review feedback: add generic Exception fallback and fix docs
ekzhu 491fa6b
Fix pylint warnings in Gemini provider tests
ekzhu 2bc4238
Fix formatting in providers router (trailing comma, black)
ekzhu c198b8c
Update gemini_provider.py
ekzhu 0979019
Merge branch 'main' into claude/add-gemini-provider-3Gy4l
xieyxclack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """A Google Gemini provider implementation using AgentScope's native | ||
| GeminiChatModel.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any, List | ||
|
|
||
| from agentscope.model import ChatModelBase | ||
| from google import genai | ||
|
|
||
| from copaw.providers.provider import ModelInfo, Provider | ||
|
|
||
|
|
||
| class GeminiProvider(Provider): | ||
| """Provider implementation for Google Gemini API.""" | ||
|
|
||
| def _client(self, timeout: float = 5) -> Any: # noqa: W0613 | ||
| _ = timeout # Gemini SDK does not support per-client timeout | ||
| return genai.Client(api_key=self.api_key) | ||
|
|
||
| @staticmethod | ||
| def _normalize_models_payload(payload: Any) -> List[ModelInfo]: | ||
| models: List[ModelInfo] = [] | ||
| for row in payload or []: | ||
| model_id = str(getattr(row, "name", "") or "").strip() | ||
| display_name = str( | ||
| getattr(row, "display_name", "") or model_id, | ||
| ).strip() | ||
|
|
||
| if not model_id: | ||
| continue | ||
|
|
||
| # Gemini API returns model names like "models/gemini-2.5-flash" | ||
| # Strip the "models/" prefix for cleaner IDs | ||
| if model_id.startswith("models/"): | ||
| model_id = model_id[len("models/") :] | ||
|
|
||
| if not display_name or display_name.startswith("models/"): | ||
| display_name = model_id | ||
ekzhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| models.append(ModelInfo(id=model_id, name=display_name)) | ||
|
|
||
| deduped: List[ModelInfo] = [] | ||
| seen: set[str] = set() | ||
| for model in models: | ||
| if model.id in seen: | ||
| continue | ||
| seen.add(model.id) | ||
| deduped.append(model) | ||
| return deduped | ||
|
|
||
| async def check_connection(self, timeout: float = 5) -> tuple[bool, str]: | ||
| """Check if Google Gemini provider is reachable.""" | ||
| try: | ||
| client = self._client(timeout=timeout) | ||
| # Use the async list models endpoint to verify connectivity | ||
| async for _ in await client.aio.models.list(): | ||
| break | ||
| return True, "" | ||
| except Exception: | ||
| return ( | ||
| False, | ||
| "Failed to connect to Google Gemini API. " | ||
| "Check your API key.", | ||
| ) | ||
ekzhu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ekzhu marked this conversation as resolved.
Show resolved
Hide resolved
ekzhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async def fetch_models(self, timeout: float = 5) -> List[ModelInfo]: | ||
| """Fetch available models from Gemini API.""" | ||
| try: | ||
| client = self._client(timeout=timeout) | ||
| payload = [] | ||
| async for model in await client.aio.models.list(): | ||
| payload.append(model) | ||
| models = self._normalize_models_payload(payload) | ||
| return models | ||
| except Exception: | ||
| return [] | ||
ekzhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| async def check_model_connection( | ||
| self, | ||
| model_id: str, | ||
| timeout: float = 5, | ||
ekzhu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) -> tuple[bool, str]: | ||
| """Check if a specific Gemini model is reachable/usable.""" | ||
| target = (model_id or "").strip() | ||
| if not target: | ||
| return False, "Empty model ID" | ||
|
|
||
| try: | ||
| client = self._client(timeout=timeout) | ||
| response = client.aio.models.generate_content_stream( | ||
| model=target, | ||
| contents="ping", | ||
| ) | ||
| async for _ in response: | ||
| break | ||
| return True, "" | ||
| except Exception: | ||
| return ( | ||
| False, | ||
| f"Model '{model_id}' is not reachable or usable", | ||
| ) | ||
ekzhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def get_chat_model_instance(self, model_id: str) -> ChatModelBase: | ||
| from agentscope.model import GeminiChatModel | ||
|
|
||
| return GeminiChatModel( | ||
| model_name=model_id, | ||
| stream=True, | ||
| api_key=self.api_key, | ||
| generate_kwargs=self.generate_kwargs, | ||
| ) | ||
ekzhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.