Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Start using `pydantic.TypeAdapter`

Lukas Kreussel19146a5c237b
diff --git a/dspy/functional/functional.py b/dspy/functional/functional.py
index 200354769..baf19b078 100644
--- a/dspy/functional/functional.py
+++ b/dspy/functional/functional.py
@@ -118,6 +118,7 @@ class TypedPredictor(dspy.Module):
def _make_example(self, type_) -> str:
# Note: DSPy will cache this call so we only pay the first time TypedPredictor is called.
+
schema = json.dumps(type_.model_json_schema())
if self.wrap_json:
schema = "```json\n" + schema + "\n```\n"
@@ -225,32 +226,21 @@ class TypedPredictor(dspy.Module):
format=lambda x: x if isinstance(x, str) else str(x),
parser=type_,
)
- elif False:
- # TODO: I don't like forcing the model to write "value" in the output.
- if not (inspect.isclass(type_) and issubclass(type_, pydantic.BaseModel)):
- type_ = pydantic.create_model("Output", value=(type_, ...), __base__=pydantic.BaseModel)
- to_json = lambda x, type_=type_: type_(value=x).model_dump_json()[9:-1] # {"value":"123"}
- from_json = lambda x, type_=type_: type_.model_validate_json('{"value":' + x + "}").value
- schema = json.dumps(type_.model_json_schema()["properties"]["value"])
- else:
- to_json = lambda x: x.model_dump_json()
- from_json = lambda x, type_=type_: type_.model_validate_json(x)
- schema = json.dumps(type_.model_json_schema())
else:
# Anything else we wrap in a pydantic object
- if not (
- inspect.isclass(type_)
+ if (inspect.isclass(type_)
and typing.get_origin(type_) not in (list, tuple) # To support Python 3.9
- and issubclass(type_, pydantic.BaseModel)
- ):
- type_ = pydantic.create_model("Output", value=(type_, ...), __base__=pydantic.BaseModel)
- to_json = lambda x, type_=type_: type_(value=x).model_dump_json()
- from_json = lambda x, type_=type_: type_.model_validate_json(x).value
- schema = json.dumps(type_.model_json_schema())
- else:
+ and issubclass(type_, pydantic.BaseModel)):
+
to_json = lambda x: x.model_dump_json()
from_json = lambda x, type_=type_: type_.model_validate_json(x)
schema = json.dumps(type_.model_json_schema())
+ else:
+ adapter = pydantic.TypeAdapter(type_)
+ to_json = lambda x : adapter.serializer.to_json(x)
+ from_json = lambda x, type_=adapter: type_.validate_json(x)
+ schema = json.dumps(adapter.json_schema())
+
if self.wrap_json:
to_json = lambda x, inner=to_json: "```json\n" + inner(x) + "\n```\n"
schema = "```json\n" + schema + "\n```"
@@ -405,9 +395,13 @@ def _func_to_signature(func):
return dspy.Signature(fields, instructions)
-def _unwrap_json(output, from_json: Callable[[str], Union[pydantic.BaseModel, str]]):
+def _unwrap_json(output, from_json: Callable[[str], Union[pydantic.BaseModel, str, None]]):
try:
- return from_json(output).model_dump_json()
+ parsing_result = from_json(output)
+ if isinstance(parsing_result, pydantic.BaseModel):
+ return parsing_result.model_dump_json()
+ else:
+ return output
except (ValueError, pydantic.ValidationError, AttributeError):
output = output.strip()
if output.startswith("```"):
@@ -416,6 +410,4 @@ def _unwrap_json(output, from_json: Callable[[str], Union[pydantic.BaseModel, st
if not output.endswith("```"):
raise ValueError("Don't write anything after the final json ```") from None
output = output[7:-3].strip()
- if not output.startswith("{") or not output.endswith("}"):
- raise ValueError("json output should start and end with { and }") from None
return ujson.dumps(ujson.loads(output)) # ujson is a bit more robust than the standard json
diff --git a/tests/functional/test_functional.py b/tests/functional/test_functional.py
index 3e6055660..7b5c032ad 100644
--- a/tests/functional/test_functional.py
+++ b/tests/functional/test_functional.py
@@ -36,7 +36,7 @@ def test_list_output():
pass
expected = ["What is the speed of light?", "What is the speed of sound?"]
- lm = DummyLM(['{"value": ["What is the speed of light?", "What is the speed of sound?"]}'])
+ lm = DummyLM(['["What is the speed of light?", "What is the speed of sound?"]'])
dspy.settings.configure(lm=lm)
question = hard_questions(topics=["Physics", "Music"])
@@ -557,7 +557,7 @@ def test_parse_type_string():
def test_literal():
- lm = DummyLM([f'{{"value": "{i}"}}' for i in range(100)])
+ lm = DummyLM([f'"{i}"' for i in range(100)])
dspy.settings.configure(lm=lm)
@predictor
@@ -568,7 +568,7 @@ def test_literal():
def test_literal_int():
- lm = DummyLM([f'{{"value": {i}}}' for i in range(100)])
+ lm = DummyLM([f'{i}' for i in range(100)])
dspy.settings.configure(lm=lm)
@predictor
@@ -897,7 +897,7 @@ def _test_demos_missing_input():
def test_conlist():
dspy.settings.configure(
- lm=DummyLM(['{"value": []}', '{"value": [1]}', '{"value": [1, 2]}', '{"value": [1, 2, 3]}'])
+ lm=DummyLM(['[]', '[1]', '[1, 2]', '[1, 2, 3]'])
)
@predictor
@@ -909,7 +909,7 @@ def test_conlist():
def test_conlist2():
dspy.settings.configure(
- lm=DummyLM(['{"value": []}', '{"value": [1]}', '{"value": [1, 2]}', '{"value": [1, 2, 3]}'])
+ lm=DummyLM(['[]', '[1]', '[1, 2]', '[1, 2, 3]'])
)
make_numbers = TypedPredictor("input:str -> output:Annotated[List[int], Field(min_items=2)]")
diff --git a/tests/functional/test_signature_opt_typed.py b/tests/functional/test_signature_opt_typed.py
index 85c8c8d56..42d7450fd 100644
--- a/tests/functional/test_signature_opt_typed.py
+++ b/tests/functional/test_signature_opt_typed.py
@@ -109,7 +109,7 @@ def test_opt():
[
# Seed prompts
"some thoughts",
- '{"value": [{"instructions": "I", "question_desc": "$q", "question_prefix": "Q:", "answer_desc": "$a", "answer_prefix": "A:"}]}',
+ '[{"instructions": "I", "question_desc": "$q", "question_prefix": "Q:", "answer_desc": "$a", "answer_prefix": "A:"}]',
]
)
dspy.settings.configure(lm=qa_model)
diff --git a/tests/functional/test_signature_typed.py b/tests/functional/test_signature_typed.py
index 56bd12748..c3d4ffb8d 100644
--- a/tests/functional/test_signature_typed.py
+++ b/tests/functional/test_signature_typed.py
@@ -1,11 +1,10 @@
-from typing import Generic, TypeVar, Any, Optional, Union
+from typing import Any, Optional, Union
import pytest
import pydantic
import dspy
-from dspy.evaluate import Evaluate
from dspy.functional import TypedPredictor
-from dspy.utils import DummyLM
+from dspy.signatures.signature import signature_to_template
def get_field_and_parser(signature:dspy.Signature) -> tuple[Any, Any]:
module = TypedPredictor(signature)
@@ -15,6 +14,35 @@ def get_field_and_parser(signature:dspy.Signature) -> tuple[Any, Any]:
parser = field.json_schema_extra.get("parser")
return field, parser
+class Mysubmodel(pydantic.BaseModel):
+ sub_floating: float
+
+class MyModel(pydantic.BaseModel):
+ floating: float
+ string: str
+ boolean: bool
+ integer: int
+ optional: Optional[str]
+ sequence_of_strings: list[str]
+ union: Union[str, float]
+ submodel: Mysubmodel
+ optional_submodel: Optional[Mysubmodel]
+ optional_existing_submodule: Optional[Mysubmodel]
+
+def build_model_instance() -> MyModel:
+ return MyModel(
+ floating=3.14,
+ string="foobar",
+ boolean=True,
+ integer=42,
+ optional=None,
+ sequence_of_strings=["foo", "bar"],
+ union=3.14,
+ submodel=Mysubmodel(sub_floating=42.42),
+ optional_submodel=None,
+ optional_existing_submodule=Mysubmodel(sub_floating=42.42),
+ )
+
@pytest.mark.parametrize("test_type,serialized, expected", [(str, "foo", "foo"), (int, "42", 42), (float, "42.42", 42.42)])
def test_basic_types(test_type:type, serialized:str, expected:Any):
@@ -38,7 +66,7 @@ def test_boolean():
assert not parser("false"), f"Parsing 'false' failed"
[email protected]("test_type,serialized, expected", [(list[str], '{"value" : ["foo", "bar"]}', ['foo', 'bar']), (tuple[int, float], '{"value":[42, 3.14]}', (42, 3.14))])
[email protected]("test_type,serialized, expected", [(list[str], '["foo", "bar"]', ['foo', 'bar']), (tuple[int, float], '[42, 3.14]', (42, 3.14))])
def test_sequences(test_type:type, serialized:str, expected:Any):
class MySignature(dspy.Signature):
question: str = dspy.InputField()
@@ -50,10 +78,10 @@ def test_sequences(test_type:type, serialized:str, expected:Any):
assert parser(serialized) == expected, f"Parsing {expected} failed"
@pytest.mark.parametrize("test_type,serialized, expected", [
- (Optional[str], '{"value": "foobar"}', "foobar"),
- (Optional[str], '{"value": null}', None),
- (Union[str, float], '{"value": 3.14}', 3.14),
- (Union[str, bool], '{"value": true}', True)
+ (Optional[str], '"foobar"', "foobar"),
+ (Optional[str], "null", None),
+ (Union[str, float], "3.14", 3.14),
+ (Union[str, bool], "true", True)
])
def test_unions(test_type:type, serialized:str, expected:Any):
class MySignature(dspy.Signature):
@@ -66,40 +94,49 @@ def test_unions(test_type:type, serialized:str, expected:Any):
def test_pydantic():
+ class MySignature(dspy.Signature):
+ question: str = dspy.InputField()
+ answer: MyModel = dspy.OutputField()
+
+ _, parser = get_field_and_parser(MySignature)
- class Mysubmodel(pydantic.BaseModel):
- sub_floating: float
+ instance = build_model_instance()
+ parsed_instance = parser(instance.model_dump_json())
+
+ assert parsed_instance == instance, f"{instance} != {parsed_instance}"
+
+def test_optional_pydantic():
+ class MySignature(dspy.Signature):
+ question: str = dspy.InputField()
+ answer: Optional[MyModel] = dspy.OutputField()
- class MyModel(pydantic.BaseModel):
- floating: float
+ _, parser = get_field_and_parser(MySignature)
+
+ instance = build_model_instance()
+ parsed_instance = parser(instance.model_dump_json())
+ assert parsed_instance == instance, f"{instance} != {parsed_instance}"
+
+ #Check null case
+ parsed_instance = parser("null")
+ assert parsed_instance == None, "Optional[MyModel] should be None"
+
+def test_dataclass():
+
+ from dataclasses import dataclass
+
+ @dataclass(frozen = True)
+ class MyDataclass:
string: str
+ number: int
+ floating: float
boolean: bool
- integer: int
- optional: Optional[str]
- sequence_of_strings: list[str]
- union: Union[str, float]
- submodel: Mysubmodel
- optional_submodel: Optional[Mysubmodel]
-
class MySignature(dspy.Signature):
question: str = dspy.InputField()
- answer: MyModel = dspy.OutputField()
+ answer: MyDataclass = dspy.OutputField()
_, parser = get_field_and_parser(MySignature)
- instance = MyModel(
- floating=3.14,
- string="foobar",
- boolean=True,
- integer=42,
- optional=None,
- sequence_of_strings=["foo", "bar"],
- union=3.14,
- submodel=Mysubmodel(sub_floating=42.42),
- optional_submodel=None
- )
-
- parser(instance.model_dump_json()) == instance, f"{instance}!= {parser(instance)}"
-
-
\ No newline at end of file
+ instance = MyDataclass("foobar", 42, 3.14, True)
+ parsed_instance = parser('{"string": "foobar", "number": 42, "floating": 3.14, "boolean": true}')
+ assert parsed_instance == instance, f"{instance} != {parsed_instance}"
\ No newline at end of file