Skip to content
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

Pydantic str type coercion from numeric types #1962

Open
fahminlb33 opened this issue Dec 19, 2024 · 2 comments
Open

Pydantic str type coercion from numeric types #1962

fahminlb33 opened this issue Dec 19, 2024 · 2 comments

Comments

@fahminlb33
Copy link

fahminlb33 commented Dec 19, 2024

Hello, I faced this simple error today about type coercion. The error is raised when an int are passed into str type in signature. Below is my signature class.

class LaptopSpecification(dspy.Signature):
    """Extracts laptop technical specification."""

    description: str = dspy.InputField()

    brand: Optional[str] = dspy.OutputField()
    series_model: Optional[str] = dspy.OutputField()
    processor: Optional[str] = dspy.OutputField()
    memory: Optional[str] = dspy.OutputField()
    storage: Optional[str] = dspy.OutputField()
    graphics_card: Optional[str] = dspy.OutputField()

Below is the result from dspy.inspect_history()

{
    "reasoning": "The laptop is suitable for general use and office work due to its Intel Core i5 processor, 16GB of RAM, and 512GB SSD storage.",
    "brand": "HP",
    "series_model": "14",
    "processor": "Intel Core i5-1235U (up to 4.4 GHz with Intel Turbo Boost Technology)",
    "memory": "16GB DDR4-3200 MHz RAM",
    "storage": "512GB PCIe NVMe M.2 SSD",
    "graphics_card": "Intel Iris Xe Graphics"
}

I guess the problem is this: because the series_model is just the string 14, the adapter incorrectly parsed it into an int and then the error is raised in this line:

return TypeAdapter(annotation).validate_python(parsed_value)

For Pydantic models, we can do coerce_numbers_to_str=True to solve this type coercion problem, but currently I can't find any documentation in DSPy that allows me to set the model_config of a signature nor the DSPy respect the model_config in the signature class.

model_config = ConfigDict(coerce_numbers_to_str=True)

For now, this is my hack to solve this number coercion problem.

return TypeAdapter(annotation, config=ConfigDict(coerce_numbers_to_str=True)).validate_python(parsed_value)

Is there a plan to allow signature class to accept a ConfigDict or specify it somewhere to control this behavior?

Thanks

Edit:
Python: 3.11
Package manager: uv 0.5.9
DSPy: 2.5.43

@okhat
Copy link
Collaborator

okhat commented Dec 19, 2024

Hey @fahminlb33 ! Thanks a lot for opening this. It looks like a bug to me, but I should check deeper.

I don't think the best fix is via coerce_numbers_to_str. We may need to change the order in which parsing happens.

@fahminlb33
Copy link
Author

I agree, the issue might lie somewhere else, and passing a custom ConfigDict is a temporary fix in this case.

I think it would be beneficial to also expose a way to set a custom ConfigDict for a signature. It would offer more control of the signature parsing behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants