Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Add Disable Fallback Option in ChatAdapter (#8984)

Noah Ziems9b467b52f1de
diff --git a/dspy/adapters/chat_adapter.py b/dspy/adapters/chat_adapter.py
index c856c2d51..8d92fa34b 100644
--- a/dspy/adapters/chat_adapter.py
+++ b/dspy/adapters/chat_adapter.py
@@ -26,6 +26,20 @@ class FieldInfoWithName(NamedTuple):
class ChatAdapter(Adapter):
+ def __init__(
+ self,
+ callbacks=None,
+ use_native_function_calling: bool = False,
+ native_response_types=None,
+ use_json_adapter_fallback: bool = True,
+ ):
+ super().__init__(
+ callbacks=callbacks,
+ use_native_function_calling=use_native_function_calling,
+ native_response_types=native_response_types,
+ )
+ self.use_json_adapter_fallback = use_json_adapter_fallback
+
def __call__(
self,
lm: LM,
@@ -40,9 +54,13 @@ class ChatAdapter(Adapter):
# fallback to JSONAdapter
from dspy.adapters.json_adapter import JSONAdapter
- if isinstance(e, ContextWindowExceededError) or isinstance(self, JSONAdapter):
- # On context window exceeded error or already using JSONAdapter, we don't want to retry with a different
- # adapter.
+ if (
+ isinstance(e, ContextWindowExceededError)
+ or isinstance(self, JSONAdapter)
+ or not self.use_json_adapter_fallback
+ ):
+ # On context window exceeded error, already using JSONAdapter, or use_json_adapter_fallback is False
+ # we don't want to retry with a different adapter. Raise the original error instead of the fallback error.
raise e
return JSONAdapter()(lm, lm_kwargs, signature, demos, inputs)
@@ -60,9 +78,13 @@ class ChatAdapter(Adapter):
# fallback to JSONAdapter
from dspy.adapters.json_adapter import JSONAdapter
- if isinstance(e, ContextWindowExceededError) or isinstance(self, JSONAdapter):
- # On context window exceeded error or already using JSONAdapter, we don't want to retry with a different
- # adapter.
+ if (
+ isinstance(e, ContextWindowExceededError)
+ or isinstance(self, JSONAdapter)
+ or not self.use_json_adapter_fallback
+ ):
+ # On context window exceeded error, already using JSONAdapter, or use_json_adapter_fallback is False
+ # we don't want to retry with a different adapter. Raise the original error instead of the fallback error.
raise e
return await JSONAdapter().acall(lm, lm_kwargs, signature, demos, inputs)
diff --git a/tests/adapters/test_chat_adapter.py b/tests/adapters/test_chat_adapter.py
index 0ea385630..51e5e065a 100644
--- a/tests/adapters/test_chat_adapter.py
+++ b/tests/adapters/test_chat_adapter.py
@@ -442,6 +442,23 @@ def test_chat_adapter_fallback_to_json_adapter_on_exception():
result = adapter(lm, {}, signature, [], {"question": "What is the capital of France?"})
assert result == [{"answer": "Paris"}]
+def test_chat_adapter_respects_use_json_adapter_fallback_flag():
+ signature = dspy.make_signature("question->answer")
+ adapter = dspy.ChatAdapter(use_json_adapter_fallback=False)
+
+ with mock.patch("litellm.completion") as mock_completion:
+ mock_completion.return_value = ModelResponse(
+ choices=[Choices(message=Message(content="nonsense"))],
+ model="openai/gpt-4o-mini",
+ )
+
+ lm = dspy.LM("openai/gpt-4o-mini", cache=False)
+
+ with mock.patch("dspy.adapters.json_adapter.JSONAdapter.__call__") as mock_json_adapter_call:
+ with pytest.raises(dspy.utils.exceptions.AdapterParseError):
+ adapter(lm, {}, signature, [], {"question": "What is the capital of France?"})
+ mock_json_adapter_call.assert_not_called()
+
@pytest.mark.asyncio
async def test_chat_adapter_fallback_to_json_adapter_on_exception_async():