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

ChatPromptBuilder Fails to JSON Serialize due to ChatMessage class #7844

Closed
lohit8846 opened this issue Jun 11, 2024 · 4 comments · Fixed by #7849
Closed

ChatPromptBuilder Fails to JSON Serialize due to ChatMessage class #7844

lohit8846 opened this issue Jun 11, 2024 · 4 comments · Fixed by #7849
Assignees
Labels
type:bug Something isn't working

Comments

@lohit8846
Copy link

Describe the bug
The new ChatPromptBuilder fails to serialize because of the ChatMessage dataclass. This component takes in a list of ChatMessages in the init method under the template

Error message
TypeError: Object of type ChatMessage is not JSON serializable

Expected behavior
The behavior I expect is that the component gets converted into JSON like this and that we can load with this JSON to create the pipeline back (I'm using a JSON Marshaller)

"components": {
    "ChatPromptBuilder": {
         "init_parameters": {
              "template": [
                   {
                        "content:" "test message"
                        "role": "user",
                        "name": "test"
                        "meta:" {
                            "test": true
                        }
                    }
              ]
         }
     }
}

Additional context
I noticed that ChatPromptBuilder and ChatMessage classes do not have any from dict or to dict functions, so it explains why they fail to serialize. In general, I've seen similar issues reported for DataClasses so I think they should all be serializable

To Reproduce

    messages = [ChatMessage.from_system("You are a helpful assistant giving out valuable information to tourists."), ChatMessage.from_user("Tell me about {{location}}")]`
   chat_prompt_builder = ChatPromptBuilder(template=messages)
@anakin87
Copy link
Member

Thanks for reporting this.

Reproducible example

from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack import Pipeline

messages = [ChatMessage.from_system("Message")]
chat_prompt_builder = ChatPromptBuilder(template=messages)

pipeline = Pipeline()
pipeline.add_component("builder", chat_prompt_builder)

pipe_str = pipeline.dumps()
new_pipeline = Pipeline().loads(pipe_str)

@anakin87 anakin87 added type:bug Something isn't working P1 High priority, add to the next sprint labels Jun 11, 2024
@CarlosFerLo
Copy link
Contributor

Let me try to solve this!

@CarlosFerLo
Copy link
Contributor

I been working on this for an hour now and I find that the problem here is that the 'component_to_dict' and 'deffault_to_dict' functions do not work recursively on fields that may need serialization, and just lives them as is. This makes serialization fail while we only use straightforward data types that should be serialized and deserialized without any problem.
In order to solve this specific bug we could just create the 'to_dict' and 'from_dict' methods of 'ChatPromptBuilder', but I believe that we could implement a process to serialize and deserialize instances recursively so we do not get this kind of errors again.

@silvanocerza
Copy link
Contributor

@CarlosFerLo go for the simple solution, custom to_dict and from_dict are the way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
5 participants