Skip to content

[Bug] generating response_format for JSONAdapter triggers an exception #8256

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

Open
poudro opened this issue May 22, 2025 · 0 comments
Open

[Bug] generating response_format for JSONAdapter triggers an exception #8256

poudro opened this issue May 22, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@poudro
Copy link

poudro commented May 22, 2025

What happened?

When generating the response format in JSONAdapter, an exception is systematically triggered and thus, under the hood, the response_format parameter sent is always {"type": "json_object"}

Steps to reproduce

Can be reproduced with

import dspy
from dspy.adapters.json_adapter import _get_structured_outputs_response_format

cot = dspy.Predict('question: str -> answer: str')
predictor = cot.predictors()[0]
response_format = _get_structured_outputs_response_format(predictor.signature)

Which triggers exception

Traceback (most recent call last):
  File "/Users/poudro/dspy/tt.py", line 6, in <module>
    response_format = _get_structured_outputs_response_format(predictor.signature)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/poudro/.pyenv/versions/dspy/lib/python3.12/site-packages/dspy/adapters/json_adapter.py", line 210, in _get_structured_outputs_response_format
    pydantic_model = pydantic.create_model(
                     ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/poudro/.pyenv/versions/dspy/lib/python3.12/site-packages/pydantic/main.py", line 1761, in create_model
    return meta(
           ^^^^^
  File "/Users/poudro/.pyenv/versions/dspy/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 110, in __new__
    config_wrapper = ConfigWrapper.for_model(bases, namespace, kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/poudro/.pyenv/versions/dspy/lib/python3.12/site-packages/pydantic/_internal/_config.py", line 138, in for_model
    config_new.update(config_from_namespace)
TypeError: 'type' object is not iterable

There is an easy fix

diff --git a/dspy/adapters/json_adapter.py b/dspy/adapters/json_adapter.py
index 018bd5f4..eb324e90 100644
--- a/dspy/adapters/json_adapter.py
+++ b/dspy/adapters/json_adapter.py
@@ -210,7 +210,7 @@ def _get_structured_outputs_response_format(signature: SignatureMeta) -> type[py
     pydantic_model = pydantic.create_model(
         "DSPyProgramOutputs",
         **fields,
-        __config__=type("Config", (), {"extra": "forbid"}),
+        __config__={"extra": "forbid"}
     )

     # Generate the initial schema.

DSPy version

2.6.24

@poudro poudro added the bug Something isn't working label May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant