Skip to content

Commit 055c519

Browse files
committed
兼容pydantic v1 和 v2
1 parent 8246085 commit 055c519

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

src/antigravity_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from .credential_manager import CredentialManager
2323
from .httpx_client import create_streaming_client_with_kwargs, http_client
24-
from .models import Model
24+
from .models import Model, model_to_dict
2525
from .utils import ANTIGRAVITY_USER_AGENT, parse_quota_reset_timestamp
2626

2727
async def _check_should_auto_ban(status_code: int) -> bool:
@@ -453,7 +453,7 @@ async def fetch_available_models(
453453
created=current_timestamp,
454454
owned_by='google'
455455
)
456-
model_list.append(model.model_dump())
456+
model_list.append(model_to_dict(model))
457457

458458
# 添加额外的 claude-opus-4-5 模型
459459
claude_opus_model = Model(
@@ -462,7 +462,7 @@ async def fetch_available_models(
462462
created=current_timestamp,
463463
owned_by='google'
464464
)
465-
model_list.append(claude_opus_model.model_dump())
465+
model_list.append(model_to_dict(claude_opus_model))
466466

467467
log.info(f"[ANTIGRAVITY] Fetched {len(model_list)} available models")
468468
return model_list

src/antigravity_router.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
GeminiGenerationConfig,
2828
Model,
2929
ModelList,
30+
model_to_dict,
3031
OpenAIChatCompletionChoice,
3132
OpenAIChatCompletionResponse,
3233
OpenAIChatMessage,
@@ -265,10 +266,8 @@ def clean_parameters(obj):
265266
func_params = function.get("parameters", {})
266267

267268
# 转换为字典(如果是 Pydantic 模型)
268-
if hasattr(func_params, "dict"):
269-
func_params = func_params.dict()
270-
elif hasattr(func_params, "model_dump"):
271-
func_params = func_params.model_dump()
269+
if hasattr(func_params, "dict") or hasattr(func_params, "model_dump"):
270+
func_params = model_to_dict(func_params)
272271

273272
# 清理参数
274273
cleaned_params = clean_parameters(func_params)
@@ -355,7 +354,7 @@ def convert_to_openai_tool_call(function_call: Dict[str, Any]) -> Dict[str, Any]
355354
arguments=json.dumps(function_call.get("args", {}))
356355
)
357356
)
358-
return tool_call.model_dump()
357+
return model_to_dict(tool_call)
359358

360359

361360
async def convert_antigravity_stream_to_openai(
@@ -652,7 +651,7 @@ def convert_antigravity_response_to_openai(
652651
usage=usage
653652
)
654653

655-
return response.model_dump()
654+
return model_to_dict(response)
656655

657656

658657
def convert_antigravity_response_to_gemini(

src/models.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
from pydantic import BaseModel, Field
44

55

6+
# Pydantic v1/v2 兼容性辅助函数
7+
def model_to_dict(model: BaseModel) -> Dict[str, Any]:
8+
"""
9+
兼容 Pydantic v1 和 v2 的模型转字典方法
10+
- v1: model.dict()
11+
- v2: model.model_dump()
12+
"""
13+
if hasattr(model, 'model_dump'):
14+
# Pydantic v2
15+
return model.model_dump()
16+
else:
17+
# Pydantic v1
18+
return model.dict()
19+
20+
621
# Common Models
722
class Model(BaseModel):
823
id: str

src/openai_transfer.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
should_include_thoughts,
2222
)
2323
from log import log
24+
from pydantic import BaseModel
2425

25-
from .models import ChatCompletionRequest
26+
from .models import ChatCompletionRequest, model_to_dict
2627

2728

2829
async def openai_request_to_gemini_payload(
@@ -778,10 +779,8 @@ def convert_openai_tools_to_gemini(openai_tools: List) -> List[Dict[str, Any]]:
778779

779780
for tool in openai_tools:
780781
# 处理 Pydantic 模型
781-
if hasattr(tool, "model_dump"):
782-
tool_dict = tool.model_dump()
783-
elif hasattr(tool, "dict"):
784-
tool_dict = tool.dict()
782+
if hasattr(tool, "model_dump") or hasattr(tool, "dict"):
783+
tool_dict = model_to_dict(tool)
785784
else:
786785
tool_dict = tool
787786

0 commit comments

Comments
 (0)