{"record":{"id":"bb6f4d8a201f4db5","repo":"microsoft/semantic-kernel","slug":"cannot-reduce-chat-history-since-the-thread-is-no-bb6f4d","errorCode":null,"errorMessage":"Cannot reduce chat history, since the thread is not currently active.","messagePattern":"Cannot reduce chat history, since the thread is not currently active\\.","errorType":"exception","errorClass":"AgentThreadOperationException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/chat_completion/chat_completion_agent.py","lineNumber":110,"sourceCode":"            self._chat_history.add_message(new_message)\n\n    async def get_messages(self) -> AsyncIterable[ChatMessageContent]:\n        \"\"\"Retrieve the current chat history.\n\n        Returns:\n            An async iterable of ChatMessageContent.\n        \"\"\"\n        if self._is_deleted:\n            raise AgentThreadOperationException(\"Cannot retrieve chat history, since the thread has been deleted.\")\n        if self._id is None:\n            await self.create()\n        for message in self._chat_history.messages:\n            yield message\n\n    async def reduce(self) -> ChatHistory | None:\n        \"\"\"Reduce the chat history to a smaller size.\"\"\"\n        if self._id is None:\n            raise AgentThreadOperationException(\"Cannot reduce chat history, since the thread is not currently active.\")\n        if not isinstance(self._chat_history, ChatHistoryReducer):\n            return None\n        return await self._chat_history.reduce()\n\n\n@register_agent_type(\"chat_completion_agent\")\nclass ChatCompletionAgent(DeclarativeSpecMixin, Agent):\n    \"\"\"A Chat Completion Agent based on ChatCompletionClientBase.\"\"\"\n\n    function_choice_behavior: FunctionChoiceBehavior | None = Field(\n        default_factory=lambda: FunctionChoiceBehavior.Auto()\n    )\n    channel_type: ClassVar[type[AgentChannel] | None] = ChatHistoryChannel\n    service: ChatCompletionClientBase | None = Field(default=None, exclude=True)\n\n    def __init__(\n        self,\n        *,","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/chat_completion/chat_completion_agent.py#L92-L128","documentation":"Raised by the chat-completion AgentThread.reduce (an AgentThreadOperationException) when self._id is None, i.e. the thread has not been created/activated yet. History reduction only applies to an active thread whose chat history is the live one.","triggerScenarios":"Calling thread.reduce() before the thread has been created (create() never awaited, or _id is still None); calling reduce on a freshly constructed thread that lazy-creates on first message but has seen no activity.","commonSituations":"Attempting summarization/reduction during setup before any message forces creation; a thread configured with a reducer but never started; reduce called in a branch where creation was skipped.","solutions":["Ensure the thread is active (await thread.create() or add a message first) before calling reduce.","Guard reduce with a check that the thread is active, or call get_response/invoke first so the thread is created.","Confirm the AgentThread is configured with a ChatHistoryReducer if you expect reduction to do work.","Reorder logic so reduction runs after the first interaction, not before creation."],"exampleFix":"// before\nreduced = await thread.reduce()  # raises: thread not active\n// after\nawait thread.create()  # or send a message first\nreduced = await thread.reduce()","handlingStrategy":"validation","validationCode":"if thread._id is None:\n    await thread.create()\nreduced = await thread.reduce()","typeGuard":"def thread_is_active(thread) -> bool:\n    return getattr(thread, '_id', None) is not None","tryCatchPattern":"from semantic_kernel.exceptions.agent_exceptions import AgentThreadOperationException\ntry:\n    reduced = await thread.reduce()\nexcept AgentThreadOperationException as e:\n    if 'not currently active' in str(e):\n        await thread.create(); reduced = await thread.reduce()\n    else: raise","preventionTips":["Create/activate the thread before reduction","Confirm a ChatHistoryReducer is configured if reduction is expected"],"tags":["chat-completion","agent-thread","history-reduction","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}