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

fix: properly serialize pydantic models #1757

Merged
merged 2 commits into from
Feb 24, 2025
Merged

Conversation

collindutter
Copy link
Member

Describe your changes

Problem:

Pydantic models are not being handled by our serialization process. This results in events that look like this:

{
│   'type': 'FinishStructureRunEvent',
│   'id': '224441bcb1c449b391576081914dd4a2',
│   'timestamp': 1740425885.337445,
│   'meta': {},
│   'structure_id': None,
│   'output_task_input': None,
│   'output_task_output': {
│   │   'type': 'ModelArtifact',
│   │   'id': '49469681d5c049139a34f95e5bdda990',
│   │   'reference': None,
│   │   'meta': {},
│   │   'name': '49469681d5c049139a34f95e5bdda990',
│   │   'value': Output(text='hello') # not serializable!
│   }
}

Solution:

  1. Introduce a ModelArtifact that is used exclusively for wrapping Pydantic models. This should be a non-breaking change since it subclasses GenericArtifact. There are hacks we could use for getting information out of GenericArtifact, but they are hacky and would require non-trivial refactors to implement.

  2. During serialization, detect instances of BaseModels and serialize them using a custom marshmallow field, fields.Model. Deserialization for this field has not been implemented at this time as it would require us to somehow store the Pydantic model with the field. I don't think users will run into this during regular usage, but the workaround would be to manually deserialize the Pydantic model outside griptape.

Serialized events now look like:

{
│   'type': 'FinishStructureRunEvent',
│   'id': '8ab4850b69534589a36cc2254612a004',
│   'timestamp': 1740426782.152982,
│   'meta': {},
│   'structure_id': None,
│   'output_task_input': None,
│   'output_task_output': {
│   │   'type': 'ModelArtifact',
│   │   'id': 'b3c96262b9be4d78bb89399321f84f72',
│   │   'reference': None,
│   │   'meta': {},
│   │   'name': 'b3c96262b9be4d78bb89399321f84f72',
│   │   'value': {'text': 'hello'} # serializable!
│   }
}

Issue ticket number and link

Closes #1756
YASW (yet another serialization woe): #1587

@collindutter collindutter self-assigned this Feb 24, 2025
Copy link

codecov bot commented Feb 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@collindutter collindutter force-pushed the fix/pydantic-serialization branch from 04ab599 to 962eb40 Compare February 24, 2025 20:24
@collindutter collindutter force-pushed the fix/pydantic-serialization branch from 962eb40 to c79f15b Compare February 24, 2025 20:29
@collindutter collindutter added this pull request to the merge queue Feb 24, 2025
Merged via the queue into main with commit ef83084 Feb 24, 2025
16 checks passed
@collindutter collindutter deleted the fix/pydantic-serialization branch February 24, 2025 20:43
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

Successfully merging this pull request may close these issues.

Properly Handle Serialization of Pydantic Models from structured output
2 participants