Skip to content

Python: ChatHistorySummarizationReducer.restore_chat_history() doesn't restore the correct service type #12513

Open
@danielescaramuzzi

Description

@danielescaramuzzi

Describe the bug
When restoring a serialized chat history using ChatHistorySummarizationReducer.restore_chat_history(), service type is not set correctly.

To Reproduce

import asyncio

from semantic_kernel import Kernel
from semantic_kernel.contents import ChatHistorySummarizationReducer
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

kernel = Kernel()
service_id = "my-service"
kernel.add_service(AzureChatCompletion(
            service_id=service_id,
            api_key="***",
            api_version="2024-10-21",
            endpoint="***",
            deployment_name="gpt-4o-mini"
        ))

chat_history = ChatHistorySummarizationReducer(
            service=kernel.get_service(service_id, AzureChatCompletion),
            target_count=2,
            threshold_count=1,
            include_function_content_in_summary=False,
        )

print(f"Service type: {type(chat_history.service)}")

chat_history.add_assistant_message("Hey there! How can I assist you today?")
chat_history.add_user_message("What is the weather like today?")
chat_history.add_assistant_message("The weather is sunny with a high of 25 degrees Celsius.") 
chat_history.add_user_message("How are you?")
chat_history.add_assistant_message("I'm fine thanks for asking! How can I help you further?") 

serialized_history = chat_history.serialize()
import json
json_serialized_history = json.loads(serialized_history)
print(f"Serialized service section: {json_serialized_history['service']}")

# restore from serialized history
restored_chat_history = ChatHistorySummarizationReducer.restore_chat_history(serialized_history)
print(f"Restored service type: {type(restored_chat_history.service)}")

async def main() -> None:
  reduced = await restored_chat_history.reduce()
  print(f"Reduced: {reduced}")

if __name__ == "__main__":
    asyncio.run(main())

produces the following output:

Service type: <class 'semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion.AzureChatCompletion'>

Serialized service section: {'ai_model_id': 'gpt-4o-mini', 'service_id': 'my-service', 'instruction_role': 'system'}

Restored service type: <class 'semantic_kernel.connectors.ai.chat_completion_client_base.ChatCompletionClientBase'>


Summarization failed, continuing without summary.
Traceback (most recent call last):
  File "***/.venv/lib/python3.12/site-packages/semantic_kernel/contents/history_reducer/chat_history_summarization_reducer.py", line 127, in reduce
    summary_msg = await self._summarize(messages_to_summarize)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/.venv/lib/python3.12/site-packages/semantic_kernel/contents/history_reducer/chat_history_summarization_reducer.py", line 166, in _summarize
    return await self.service.get_chat_message_content(chat_history=chat_history, settings=execution_settings)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/chat_completion_client_base.py", line 190, in get_chat_message_content
    results = await self.get_chat_message_contents(chat_history=chat_history, settings=settings, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/chat_completion_client_base.py", line 113, in get_chat_message_contents
    return await self._inner_get_chat_message_contents(chat_history, settings)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/.venv/lib/python3.12/site-packages/semantic_kernel/connectors/ai/chat_completion_client_base.py", line 58, in _inner_get_chat_message_contents
    raise NotImplementedError("The _inner_get_chat_message_contents method is not implemented.")
NotImplementedError: The _inner_get_chat_message_contents method is not implemented.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "***/./bug_restore_history.py", line 46, in <module>
    asyncio.run(main())
  File "***/.pyenv/versions/3.12.3/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "***/.pyenv/versions/3.12.3/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/.pyenv/versions/3.12.3/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "***/./bug_restore_history.py", line 42, in main
    reduced = await restored_chat_history.reduce()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "***/.venv/lib/python3.12/site-packages/semantic_kernel/contents/history_reducer/chat_history_summarization_reducer.py", line 149, in reduce
    raise ChatHistoryReducerException("Chat History Summarization failed.") from ex
semantic_kernel.exceptions.content_exceptions.ChatHistoryReducerException: Chat History Summarization failed.

Expected behavior
When serializing and deserializing chat history, service type should not change.

Platform

  • Language: Python
  • Version: semantic-kernel==1.33.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpythonPull requests for the Python Semantic Kernel

Type

Projects

Status

Bug

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions