Skip to content

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

Open
@poudro

Description

@poudro

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions