From 53d7c3b6da3a3f1d0488a923eebd0cd64cc26e44 Mon Sep 17 00:00:00 2001 From: amstrongzyf <201840057@smail.nju.edu.cn> Date: Wed, 9 Jul 2025 18:40:48 +0800 Subject: [PATCH] fix: fix a small bug in response_schema (#1043) * fix: fix a small bug in response_schema * black fix --- rdagent/oai/backend/base.py | 7 ++++--- rdagent/oai/backend/litellm.py | 2 ++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/rdagent/oai/backend/base.py b/rdagent/oai/backend/base.py index 03348754..9c3f6db9 100644 --- a/rdagent/oai/backend/base.py +++ b/rdagent/oai/backend/base.py @@ -575,12 +575,13 @@ class APIBackend(ABC): _, all_response = match.groups() if match else ("", all_response) # 3) format checking - if json_mode: + if json_mode or json_target_type: parser = JSONParser() all_response = parser.parse(all_response) + if json_target_type: + # deepseek will enter this branch + TypeAdapter(json_target_type).validate_json(all_response) - if json_target_type is not None: - TypeAdapter(json_target_type).validate_json(all_response) if (response_format := kwargs.get("response_format")) is not None: if not isinstance(response_format, dict) and issubclass(response_format, BaseModel): # It may raise TypeError if initialization fails diff --git a/rdagent/oai/backend/litellm.py b/rdagent/oai/backend/litellm.py index 82f6b8bb..f3740ef9 100644 --- a/rdagent/oai/backend/litellm.py +++ b/rdagent/oai/backend/litellm.py @@ -95,7 +95,9 @@ class LiteLLMAPIBackend(APIBackend): """ if json_mode and supports_response_schema(model=LITELLM_SETTINGS.chat_model): kwargs["response_format"] = {"type": "json_object"} + elif not supports_response_schema(model=LITELLM_SETTINGS.chat_model) and "response_format" in kwargs: + # Deepseek will enter this branch logger.warning( f"{LogColors.RED}Model {LITELLM_SETTINGS.chat_model} does not support response schema, ignoring response_format argument.{LogColors.END}", tag="llm_messages",