I want to create a new chat_ctx from scratch, can ...
# ask-ai
l
I want to create a new chat_ctx from scratch, can you tell me how to fix this? (i think it's the
new_message_history = []
)
Copy code
if context_forwarding_type == ContextForwardingType.NO_HISTORY:
            # Create a new chat history with only the new system message
            new_message_history = []

            for message in self.agent.chat_ctx.messages:
                if message.role == "system":
                    # Generate updated prompt message
                    updated_message = message.copy()
                    updated_message.content = current_node.prompt

                    # Append the new message at the end
                    new_message_history.append(updated_message)

                    # Remove the original message
                    new_message_history.remove(message)

                    self.agent.chat_ctx.messages = new_message_history
g
It could be possible that the issue lies in how the
new_message_history
is being manipulated. The code attempts to remove a message from
new_message_history
that was never added to it, which might be causing an error. Instead of removing the original message, you should ensure that only the updated message is added to
new_message_history
. Here's a revised version of your code:
Copy code
if context_forwarding_type == ContextForwardingType.NO_HISTORY:
    # Create a new chat history with only the new system message
    new_message_history = []

    for message in self.agent.chat_ctx.messages:
        if message.role == "system":
            # Generate updated prompt message
            updated_message = message.copy()
            updated_message.content = current_node.prompt

            # Append the new message at the end
            new_message_history.append(updated_message)

    # Set the new message history
    self.agent.chat_ctx.messages = new_message_history
This code ensures that only the updated system messages are added to
new_message_history
, and then it sets
self.agent.chat_ctx.messages
to this new list. Would you like more details on this approach? Sources: - https://docs.livekit.io/python/livekit/agents/llm/chat_context.html - https://livekit-users.slack.com/archives/C07FY8WHGPM/p1732170619.318249 Note: I wasn’t able to find highly relevant data sources, but above are a few potentially relevant links. 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.