semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

Python: Bug: [PYTHON] Chat history serializing error

Open nguyena2 opened this issue 1 year ago • 3 comments

Describe the bug I have a user message and an azure open ai assistant message in the chat history. I'm trying to serialize the chat history object to a json string so I can save it. When I try to use the serialize function in the chat history class, I get the error below.

"Error: Unable to serialize ChatHistory to JSON: Error serializing to JSON: TypeError: 'MockValSer' object cannot be converted to 'SchemaSerializer'"

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior Get a json string

Screenshots If applicable, add screenshots to help explain your problem.

Platform

  • OS: Windows
  • IDE: VS Code
  • Language: Python
  • Source: pip package version 1.1.2

Additional context Add any other context about the problem here.

nguyena2 avatar Jul 17 '24 22:07 nguyena2

Hi @nguyena2 are you able to provide us with more information (possibly the entire chat history if it doesn't contain sensitive info) about what is contained in the chat history and why the serialize/model dump would be failing?

moonbox3 avatar Jul 18 '24 13:07 moonbox3

@moonbox3 @markwallace-microsoft I can confirm that we are seeing this issue (or something similar) as well.

pf.flow.test failed with UserErrorException: PydanticSerializationError: Execution failure in 'agent': (PydanticSerializationError) Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class 'type'>

I am able to replicate this in the function calling stepwise planner starting in Semantic Kernel 1.2.0 simply by adding the SK Core Time Plugin to the kernel and asking a question that invokes any of those functions.

At the end of the process we use ChatHistory.model_dump_json() (same as is being called in ChatHistory.Serialize()) to output the steps taken by the planner and this is what is triggering the error. I am able to successfully use the ChatHistory.model_dump() to explore the Chat History object and I have not been able to identify anything about the payload itself that would be causing the serialization error.

I have replicated the behavior in Python 3.11 and 3.12, and can confirm that the issue can be resolved by moving the project back to Semantic Kernel 1.1.2.

If y'all feel that this is a separate issue, I am happy to document it in another Github Issue and I am available to demonstrate my example if so desired.

denny-nolan-kochcc avatar Jul 19 '24 20:07 denny-nolan-kochcc

@moonbox3 @markwallace-microsoft I can confirm that we are seeing this issue (or something similar) as well.

pf.flow.test failed with UserErrorException: PydanticSerializationError: Execution failure in 'agent': (PydanticSerializationError) Error serializing to JSON: PydanticSerializationError: Unable to serialize unknown type: <class 'type'>

I am able to replicate this in the function calling stepwise planner starting in Semantic Kernel 1.2.0 simply by adding the SK Core Time Plugin to the kernel and asking a question that invokes any of those functions.

At the end of the process we use ChatHistory.model_dump_json() (same as is being called in ChatHistory.Serialize()) to output the steps taken by the planner and this is what is triggering the error. I am able to successfully use the ChatHistory.model_dump() to explore the Chat History object and I have not been able to identify anything about the payload itself that would be causing the serialization error.

I have replicated the behavior in Python 3.11 and 3.12, and can confirm that the issue can be resolved by moving the project back to Semantic Kernel 1.1.2.

If y'all feel that this is a separate issue, I am happy to document it in another Github Issue and I am available to demonstrate my example if so desired.

Thanks for the extra context, @denny-nolan-kochcc. Tagging @eavanvalkenburg for visibility.

moonbox3 avatar Jul 19 '24 21:07 moonbox3

@nguyena2 and @denny-nolan-kochcc the issue is caused by a change we made so that the result in a FunctionResultContent can now be any type, instead of before where we cast everything to a string, which was limiting other use cases, so could you check the types used internally that are being put in there, you might also do [msg.to_dict() for msg in chat_history.messages] for inspection purposes, while I work on a fix.

eavanvalkenburg avatar Jul 22 '24 15:07 eavanvalkenburg

@nguyena2 and @denny-nolan-kochcc the issue is caused by a change we made so that the result in a FunctionResultContent can now be any type, instead of before where we cast everything to a string, which was limiting other use cases, so could you check the types used internally that are being put in there, you might also do [msg.to_dict() for msg in chat_history.messages] for inspection purposes, while I work on a fix.

image

nguyena2 avatar Jul 22 '24 16:07 nguyena2

@nguyena2 and @denny-nolan-kochcc the issue is caused by a change we made so that the result in a FunctionResultContent can now be any type, instead of before where we cast everything to a string, which was limiting other use cases, so could you check the types used internally that are being put in there, you might also do [msg.to_dict() for msg in chat_history.messages] for inspection purposes, while I work on a fix.

image

what are we looking at in this screen @nguyena2 ?

eavanvalkenburg avatar Jul 23 '24 11:07 eavanvalkenburg

These are the messages.to_dict(), how do I get the types?

nguyena2 avatar Jul 23 '24 14:07 nguyena2

Hello, could you tell us when you are going to fix that? Thanks,

bssoufo avatar Jul 24 '24 04:07 bssoufo

What's the recommended approach for a work around until this gets fixed?

nguyena2 avatar Jul 24 '24 16:07 nguyena2