Add explanatory comments for LLMSpecificMessage guards in context summarization, amd fixed the missing guard in LLMContextSummarizer._apply_summary when searching for the first system message.
This commit is contained in:
@@ -18,7 +18,7 @@ from pipecat.frames.frames import (
|
|||||||
LLMContextSummaryResultFrame,
|
LLMContextSummaryResultFrame,
|
||||||
LLMFullResponseStartFrame,
|
LLMFullResponseStartFrame,
|
||||||
)
|
)
|
||||||
from pipecat.processors.aggregators.llm_context import LLMContext
|
from pipecat.processors.aggregators.llm_context import LLMContext, LLMSpecificMessage
|
||||||
from pipecat.utils.asyncio.task_manager import BaseTaskManager
|
from pipecat.utils.asyncio.task_manager import BaseTaskManager
|
||||||
from pipecat.utils.base_object import BaseObject
|
from pipecat.utils.base_object import BaseObject
|
||||||
from pipecat.utils.context.llm_context_summarization import (
|
from pipecat.utils.context.llm_context_summarization import (
|
||||||
@@ -290,8 +290,18 @@ class LLMContextSummarizer(BaseObject):
|
|||||||
"""
|
"""
|
||||||
messages = self._context.messages
|
messages = self._context.messages
|
||||||
|
|
||||||
# Find the first system message to preserve
|
# Find the first system message to preserve. LLMSpecificMessage instances are excluded
|
||||||
first_system_msg = next((msg for msg in messages if msg.get("role") == "system"), None)
|
# because they are not dict-like and never represent a system message; they hold
|
||||||
|
# service-specific metadata (e.g. thinking blocks) that is always paired with a
|
||||||
|
# standard message.
|
||||||
|
first_system_msg = next(
|
||||||
|
(
|
||||||
|
msg
|
||||||
|
for msg in messages
|
||||||
|
if not isinstance(msg, LLMSpecificMessage) and msg.get("role") == "system"
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
|
||||||
# Get recent messages to keep
|
# Get recent messages to keep
|
||||||
recent_messages = messages[last_summarized_index + 1 :]
|
recent_messages = messages[last_summarized_index + 1 :]
|
||||||
|
|||||||
@@ -188,6 +188,8 @@ class LLMContextSummarizationUtil:
|
|||||||
total = 0
|
total = 0
|
||||||
|
|
||||||
for message in context.messages:
|
for message in context.messages:
|
||||||
|
# LLMSpecificMessage holds service-specific data (e.g. thinking blocks,
|
||||||
|
# thought signatures). Skipping them here for now.
|
||||||
if isinstance(message, LLMSpecificMessage):
|
if isinstance(message, LLMSpecificMessage):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -251,6 +253,9 @@ class LLMContextSummarizationUtil:
|
|||||||
|
|
||||||
for i in range(start_idx, len(messages)):
|
for i in range(start_idx, len(messages)):
|
||||||
msg = messages[i]
|
msg = messages[i]
|
||||||
|
# LLMSpecificMessage instances (e.g. thinking blocks) never carry tool_call or
|
||||||
|
# tool_call_id fields, so they cannot affect the pending-call tracking. Skipping
|
||||||
|
# them avoids an AttributeError.
|
||||||
if isinstance(msg, LLMSpecificMessage):
|
if isinstance(msg, LLMSpecificMessage):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -302,7 +307,10 @@ class LLMContextSummarizationUtil:
|
|||||||
if len(messages) <= min_messages_to_keep:
|
if len(messages) <= min_messages_to_keep:
|
||||||
return LLMMessagesToSummarize(messages=[], last_summarized_index=-1)
|
return LLMMessagesToSummarize(messages=[], last_summarized_index=-1)
|
||||||
|
|
||||||
# Find first system message index
|
# Find first system message index. LLMSpecificMessage instances are excluded because
|
||||||
|
# they are not dict-like and never represent a system message; they hold
|
||||||
|
# service-specific metadata (e.g. thinking blocks) that is always paired with a
|
||||||
|
# standard message.
|
||||||
first_system_index = next(
|
first_system_index = next(
|
||||||
(
|
(
|
||||||
i
|
i
|
||||||
@@ -367,6 +375,11 @@ class LLMContextSummarizationUtil:
|
|||||||
transcript_parts = []
|
transcript_parts = []
|
||||||
|
|
||||||
for msg in messages:
|
for msg in messages:
|
||||||
|
# LLMSpecificMessage holds service-specific internal data (e.g. Anthropic thinking
|
||||||
|
# blocks, Gemini thought signatures). This data is not meaningful as plain text for
|
||||||
|
# a summarization transcript, and the summarizer LLM would not know how to interpret
|
||||||
|
# it. The conversational content of those turns is already captured by the
|
||||||
|
# accompanying standard assistant message.
|
||||||
if isinstance(msg, LLMSpecificMessage):
|
if isinstance(msg, LLMSpecificMessage):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user