fix(pipecat): keep memory text literal when replacing the injected block - #1647
Open
Agnik47 wants to merge 1 commit into
Open
fix(pipecat): keep memory text literal when replacing the injected block#1647Agnik47 wants to merge 1 commit into
Agnik47 wants to merge 1 commit into
Conversation
_enhance_context_with_memories refreshes the system message by calling MEMORY_TAG_PATTERN.sub(tagged_memory, existing_content). Passing the memory text as a string replacement makes re.sub parse it as a template, so backslash sequences in ordinary memories are expanded rather than inserted. Two failures follow, both on the second and later turns, once a block exists to replace: "User's repo is at C:\Users\alice" -> re.error: bad escape \U "User writes \1 for a capture group" -> re.error: invalid group reference 1 The exception propagates out of context enhancement and aborts the turn. A callable replacement inserts the text verbatim and cannot be parsed as a template. Adds a regression test covering the Windows-path case, which fails on main with the production error.
TracePull @yesprasad Reviewed this PR — memory injection escape safetyDecisionNo actionable issues found. What changedThis PR fixes system-memory replacement when retrieved memory contains backslashes or regex-style group references.
Verified flowflowchart LR
A[Retrieved memory facts] --> B[Build tagged_memory]
B --> C[Existing system message]
C --> D{Memory tag exists?}
D -->|Yes| E[Callable re.sub replacement]
E --> F[Updated system prompt]
D -->|No| G[Append tagged memory]
G --> F
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
_enhance_context_with_memoriesrefreshes the<user_memories>block in the system message like this:re.sub()parses a string replacement as a template. Backslash sequences intagged_memoryare therefore expanded as group references rather than inserted, andMEMORY_TAG_PATTERNhas no groups at all — so ordinary memory content raises:The exception propagates out of context enhancement and aborts the turn. It only fires from the second turn onward, once a block exists to replace — the exact path this method's docstring describes:
A memory holding a Windows path is enough to trigger it, and memory text is arbitrary user content.
escape_memory_delimitersdoes not help here: it neutralizes<user_memories>tags, not backslashes.Fix
Use a callable replacement, which
re.sub()inserts verbatim and never parses as a template:This is the only dynamic-replacement
re.sub()in the repo — every other call site in the Python SDKs already passes a callable or a literal"", so this brings the last one in line.Tests
Adds
test_system_injection_replaces_stale_block_with_backslash_memoryto the existing pipecat suite: a stale block plus a memory containing a Windows path, asserting the new fact lands, the stale one is gone, the caller's own system text survives, and exactly one block remains.On
mainit fails with the production error (re.PatternError: bad escape \U); with the fix the suite is 3/3.ci-python.ymlruns the pipecat suite on any PR touchingpackages/pipecat-sdk-python/**, so this is covered on both the 3.10 and 3.12 lanes.Scope
One functional line plus a regression test. No API surface change, no new dependencies, no behaviour change for memories without backslashes.