Skip to content

Conversation

cuoicungtui
Copy link

Why are these changes needed?

✨ Pull Request: Add MultiChatCompletionContext and MergeSystemChatCompletionContext

🧩 Overview

I’m currently using AutoGen as the main framework for my project.
However, during development, I encountered several limitations when self-hosting custom LLM models.


⚙️ Motivation

1️⃣ Context Flexibility

Using only a single type of ChatCompletionContext within an agent (such as UnboundedChatCompletionContext or BufferedChatCompletionContext) is sometimes not sufficient to safely handle different model limitations.

While TokenLimitedChatCompletionContext can help avoid issues related to exceeding the model’s context window, relying on just one context strategy still lacks flexibility for more complex use cases.

To address this, I propose adding MultiChatCompletionContext, which allows combining multiple context management strategies dynamically, providing safer and more adaptable message handling for both large and small models.


2️⃣ Multi-System Prompt Handling

When using the memory feature within AssistantAgent, I encountered the following error:

ValueError: Multiple and Not continuous system messages are not supported if model_info['multiple_system_messages'] is False

This occurs because, even though the model is defined as not supporting multiple_system_messages, the AssistantAgent automatically creates new SystemMessage entries to store memory information.
As a result, the agent unintentionally produces multiple system prompts, which leads to the above error.

Currently, there is no straightforward way to prevent this behavior through configuration.

To solve this, I introduced MergeSystemChatCompletionContext, which automatically merges multiple system prompts into a single unified message — ensuring compatibility with models that do not support multiple system messages.


🧠 What’s New

  • Added MultiChatCompletionContext

    • Enables combining multiple message management strategies
    • Helps avoid “out of context” errors for self-hosted or small-window models
  • Added MergeSystemChatCompletionContext

    • Automatically merges multiple system prompts into one
    • Prevents ValueError when multiple_system_messages is disabled
    • Improves compatibility with models that don’t support multiple system messages

🧪 Example Use Case

from autogen_core.model_context import MultiChatCompletionContext, MergeSystemChatCompletionContext

context = MultiChatCompletionContext([
    TokenLimitedChatCompletionContext(limit=1800),
    MergeSystemChatCompletionContext(),
])

💬 Summary

This PR aims to improve flexibility and stability for developers using AutoGen with self-hosted or restricted LLMs.

It introduces more control over how contexts and system prompts are managed — reducing the need for users to build complex wrappers and ensuring safer handling of memory and multi-system configurations.

Related issue number

Checks

@cuoicungtui
Copy link
Author

cuoicungtui commented Oct 19, 2025

@microsoft-github-policy-service agree

@cuoicungtui cuoicungtui changed the title Feature/chat multi context Feature/Multi Model Context Oct 19, 2025
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.

1 participant