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

Some typed inputs fail JSON serialization #1826

Closed
eabruzzese opened this issue Nov 19, 2024 · 3 comments · Fixed by #1853
Closed

Some typed inputs fail JSON serialization #1826

eabruzzese opened this issue Nov 19, 2024 · 3 comments · Fixed by #1853
Assignees

Comments

@eabruzzese
Copy link

Context: I have a module that generates a structured output with one program, and then passes that object (a Pydantic model) as an input to the next program.

If the input object contains a field that's not natively JSON serializable (in my example, a datetime), a TypeError is thrown.

Here's a small example:

import os
import dspy
from datetime import datetime
from pydantic import BaseModel, Field

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "openai/gpt-3.5-turbo")

dspy.configure(
    lm=dspy.LM(
        OPENAI_MODEL,
        api_key=OPENAI_API_KEY,
        cache=False,
    )
)


class Input(BaseModel):
    message: str = Field(desc="The message to be parsed.")

    # This field will cause a failure in the form of a TypeError, since
    # datetimes are not JSON serializable. If you comment this out, the example
    # will work as expected.
    some_datetime: datetime = Field(desc="A random datetime.")


class Output(BaseModel):
    # Output fields work as expected.
    start_at: datetime = Field(desc="The datetime at which the event starts.")
    end_at: datetime = Field(desc="The datetime at which the event ends.")


class Signature(dspy.Signature):
    input: Input = dspy.InputField(desc="A description of the event to be parsed.")
    output: Output = dspy.OutputField(desc="The start and end times of the event.")


event_parser = dspy.Predict(Signature)
result = event_parser(
    input=Input(
        message="My birthday party will be 11/20/2024 from 7pm to 10pm",
        some_datetime=datetime.now(),
    )
)

# The above invocation results in "TypeError: Object of type datetime is not JSON serializable"

print(result.output.model_dump_json(indent=2))
@okhat
Copy link
Collaborator

okhat commented Nov 19, 2024

Thanks so much for the report, @eabruzzese ! I think something similar was indeed observed by @dbczumar who was also considering better approaches for this.

@dbczumar
Copy link
Collaborator

Hi @eabruzzese , I'll have a fix out for this shortly :)

@eabruzzese
Copy link
Author

Woah that's awesome. Thanks @dbczumar!

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