Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

allow error to pass through in JSONAdapter (#8445)

Chen Qian734eff216155
diff --git a/dspy/adapters/json_adapter.py b/dspy/adapters/json_adapter.py
index 5e599d29c..9b14691c7 100644
--- a/dspy/adapters/json_adapter.py
+++ b/dspy/adapters/json_adapter.py
@@ -22,11 +22,6 @@ from dspy.utils.exceptions import AdapterParseError
logger = logging.getLogger(__name__)
-ERROR_MESSAGE_ON_JSON_ADAPTER_FAILURE = (
- "Both structured output format and JSON mode failed. Please choose a model that supports "
- "`response_format` argument. Original error: {}"
-)
-
def _has_open_ended_mapping(signature: SignatureMeta) -> bool:
"""
@@ -72,13 +67,8 @@ class JSONAdapter(ChatAdapter):
return super().__call__(lm, lm_kwargs, signature, demos, inputs)
except Exception:
logger.warning("Failed to use structured output format, falling back to JSON mode.")
- try:
- lm_kwargs["response_format"] = {"type": "json_object"}
- return super().__call__(lm, lm_kwargs, signature, demos, inputs)
- except AdapterParseError as e:
- raise e
- except Exception as e:
- raise RuntimeError(ERROR_MESSAGE_ON_JSON_ADAPTER_FAILURE.format(e)) from e
+ lm_kwargs["response_format"] = {"type": "json_object"}
+ return super().__call__(lm, lm_kwargs, signature, demos, inputs)
async def acall(
self,
@@ -98,13 +88,8 @@ class JSONAdapter(ChatAdapter):
return await super().acall(lm, lm_kwargs, signature, demos, inputs)
except Exception:
logger.warning("Failed to use structured output format, falling back to JSON mode.")
- try:
- lm_kwargs["response_format"] = {"type": "json_object"}
- return await super().acall(lm, lm_kwargs, signature, demos, inputs)
- except AdapterParseError as e:
- raise e
- except Exception as e:
- raise RuntimeError(ERROR_MESSAGE_ON_JSON_ADAPTER_FAILURE.format(e)) from e
+ lm_kwargs["response_format"] = {"type": "json_object"}
+ return await super().acall(lm, lm_kwargs, signature, demos, inputs)
def _call_preprocess(
self,
diff --git a/tests/adapters/test_json_adapter.py b/tests/adapters/test_json_adapter.py
index f688af43f..eb90a754d 100644
--- a/tests/adapters/test_json_adapter.py
+++ b/tests/adapters/test_json_adapter.py
@@ -5,7 +5,6 @@ import pytest
from litellm.utils import Choices, Message, ModelResponse
import dspy
-from dspy.adapters.json_adapter import ERROR_MESSAGE_ON_JSON_ADAPTER_FAILURE
def test_json_adapter_passes_structured_output_when_supported_by_model():
@@ -616,12 +615,18 @@ def test_error_message_on_json_adapter_failure():
dspy.configure(lm=dspy.LM(model="openai/gpt-4o-mini", cache=False), adapter=dspy.JSONAdapter())
with mock.patch("litellm.completion") as mock_completion:
- mock_completion.side_effect = RuntimeError("Failed!")
+ mock_completion.side_effect = RuntimeError("RuntimeError!")
with pytest.raises(RuntimeError) as error:
program(question="Dummy question!")
- assert ERROR_MESSAGE_ON_JSON_ADAPTER_FAILURE[:50] in str(error.value)
+ assert "RuntimeError!" in str(error.value)
+
+ mock_completion.side_effect = ValueError("ValueError!")
+ with pytest.raises(ValueError) as error:
+ program(question="Dummy question!")
+
+ assert "ValueError!" in str(error.value)
@pytest.mark.asyncio
@@ -633,10 +638,15 @@ async def test_error_message_on_json_adapter_failure_async():
program = dspy.Predict(TestSignature)
with mock.patch("litellm.acompletion") as mock_acompletion:
- mock_acompletion.side_effect = RuntimeError("Failed!")
-
with dspy.context(lm=dspy.LM(model="openai/gpt-4o-mini", cache=False), adapter=dspy.JSONAdapter()):
+ mock_acompletion.side_effect = RuntimeError("RuntimeError!")
with pytest.raises(RuntimeError) as error:
await program.acall(question="Dummy question!")
- assert ERROR_MESSAGE_ON_JSON_ADAPTER_FAILURE[:50] in str(error.value)
+ assert "RuntimeError!" in str(error.value)
+
+ mock_acompletion.side_effect = ValueError("ValueError!")
+ with pytest.raises(ValueError) as error:
+ await program.acall(question="Dummy question!")
+
+ assert "ValueError!" in str(error.value)