Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix: patch send message partial json extractor (#2868)

Charles Packeraf62e22b769c
diff --git a/letta/interfaces/openai_streaming_interface.py b/letta/interfaces/openai_streaming_interface.py
index 4bedbc719..0ae0a4be5 100644
--- a/letta/interfaces/openai_streaming_interface.py
+++ b/letta/interfaces/openai_streaming_interface.py
@@ -24,7 +24,11 @@ from letta.schemas.letta_stop_reason import LettaStopReason, StopReasonType
from letta.schemas.message import Message
from letta.schemas.openai.chat_completion_response import FunctionCall, ToolCall
from letta.server.rest_api.json_parser import OptimisticJSONParser
-from letta.streaming_utils import FunctionArgumentsStreamHandler, JSONInnerThoughtsExtractor
+from letta.streaming_utils import (
+ FunctionArgumentsStreamHandler,
+ JSONInnerThoughtsExtractor,
+ sanitize_streamed_message_content,
+)
from letta.utils import count_tokens
logger = get_logger(__name__)
@@ -278,8 +282,6 @@ class OpenAIStreamingInterface:
self.prev_assistant_message_id = self.function_id_buffer
# Reset message reader at the start of a new send_message stream
self.assistant_message_json_reader.reset()
- self.assistant_message_json_reader.in_message = True
- self.assistant_message_json_reader.message_started = True
else:
if prev_message_type and prev_message_type != "tool_call_message":
@@ -334,8 +336,15 @@ class OpenAIStreamingInterface:
self.last_flushed_function_name is not None
and self.last_flushed_function_name == self.assistant_message_tool_name
):
- # Minimal, robust extraction: only emit the value of "message"
- extracted = self.assistant_message_json_reader.process_json_chunk(tool_call.function.arguments)
+ # Minimal, robust extraction: only emit the value of "message".
+ # If we buffered a prefix while name was streaming, feed it first.
+ if self.function_args_buffer:
+ payload = self.function_args_buffer + tool_call.function.arguments
+ self.function_args_buffer = None
+ else:
+ payload = tool_call.function.arguments
+ extracted = self.assistant_message_json_reader.process_json_chunk(payload)
+ extracted = sanitize_streamed_message_content(extracted or "")
if extracted:
if prev_message_type and prev_message_type != "assistant_message":
message_index += 1
diff --git a/letta/server/rest_api/interface.py b/letta/server/rest_api/interface.py
index b76c78a3e..c9ca51664 100644
--- a/letta/server/rest_api/interface.py
+++ b/letta/server/rest_api/interface.py
@@ -808,86 +808,33 @@ class StreamingServerInterface(AgentChunkStreamingInterface):
# If there was nothing in the name buffer, we can proceed to
# output the arguments chunk as a ToolCallMessage
else:
- # use_assisitant_message means that we should also not release main_json raw, and instead should only release the contents of "message": "..."
+ # use_assistant_message means we should emit only the value of "message"
if self.use_assistant_message and (
self.last_flushed_function_name is not None
and self.last_flushed_function_name == self.assistant_message_tool_name
):
- # do an additional parse on the updates_main_json
- if self.function_args_buffer:
- updates_main_json = self.function_args_buffer + updates_main_json
- self.function_args_buffer = None
-
- # Pretty gross hardcoding that assumes that if we're toggling into the keywords, we have the full prefix
- match_str = '{"' + self.assistant_message_tool_kwarg + '":"'
- if updates_main_json == match_str:
- updates_main_json = None
-
- else:
- # Some hardcoding to strip off the trailing "}"
- if updates_main_json in ["}", '"}']:
- updates_main_json = None
- if updates_main_json and len(updates_main_json) > 0 and updates_main_json[-1:] == '"':
- updates_main_json = updates_main_json[:-1]
-
- if not updates_main_json:
- # early exit to turn into content mode
+ # Feed any buffered prefix first to avoid missing the start of the value
+ payload = (self.function_args_buffer or "") + (updates_main_json or "")
+ self.function_args_buffer = None
+ cleaned = self.streaming_chat_completion_json_reader.process_json_chunk(payload)
+ from letta.streaming_utils import sanitize_streamed_message_content
+
+ cleaned = sanitize_streamed_message_content(cleaned or "")
+ if not cleaned:
return None
-
- # There may be a buffer from a previous chunk, for example
- # if the previous chunk had arguments but we needed to flush name
- if self.function_args_buffer:
- # In this case, we should release the buffer + new data at once
- combined_chunk = self.function_args_buffer + updates_main_json
-
- if prev_message_type and prev_message_type != "assistant_message":
- message_index += 1
- processed_chunk = AssistantMessage(
- id=message_id,
- date=message_date,
- content=combined_chunk,
- name=name,
- otid=Message.generate_otid_from_id(message_id, message_index),
- )
- # Store the ID of the tool call so allow skipping the corresponding response
- if self.function_id_buffer:
- self.prev_assistant_message_id = self.function_id_buffer
- # clear buffer
- self.function_args_buffer = None
- self.function_id_buffer = None
-
- else:
- # If there's no buffer to clear, just output a new chunk with new data
- # TODO: THIS IS HORRIBLE
- # TODO: WE USE THE OLD JSON PARSER EARLIER (WHICH DOES NOTHING) AND NOW THE NEW JSON PARSER
- # TODO: THIS IS TOTALLY WRONG AND BAD, BUT SAVING FOR A LARGER REWRITE IN THE NEAR FUTURE
- parsed_args = self.optimistic_json_parser.parse(self.current_function_arguments)
-
- if parsed_args.get(self.assistant_message_tool_kwarg) and parsed_args.get(
- self.assistant_message_tool_kwarg
- ) != self.current_json_parse_result.get(self.assistant_message_tool_kwarg):
- new_content = parsed_args.get(self.assistant_message_tool_kwarg)
- prev_content = self.current_json_parse_result.get(self.assistant_message_tool_kwarg, "")
- # TODO: Assumes consistent state and that prev_content is subset of new_content
- diff = new_content.replace(prev_content, "", 1)
- self.current_json_parse_result = parsed_args
- if prev_message_type and prev_message_type != "assistant_message":
- message_index += 1
- processed_chunk = AssistantMessage(
- id=message_id,
- date=message_date,
- content=diff,
- name=name,
- otid=Message.generate_otid_from_id(message_id, message_index),
- )
- else:
- return None
-
- # Store the ID of the tool call so allow skipping the corresponding response
- if self.function_id_buffer:
- self.prev_assistant_message_id = self.function_id_buffer
- # clear buffers
- self.function_id_buffer = None
+ if prev_message_type and prev_message_type != "assistant_message":
+ message_index += 1
+ processed_chunk = AssistantMessage(
+ id=message_id,
+ date=message_date,
+ content=cleaned,
+ name=name,
+ otid=Message.generate_otid_from_id(message_id, message_index),
+ )
+ # Store the ID of the tool call so allow skipping the corresponding response
+ if self.function_id_buffer:
+ self.prev_assistant_message_id = self.function_id_buffer
+ # Do not clear function_id_buffer here — we may still need it
else:
# There may be a buffer from a previous chunk, for example
# if the previous chunk had arguments but we needed to flush name
diff --git a/letta/streaming_utils.py b/letta/streaming_utils.py
index a34f45778..f157ea01b 100644
--- a/letta/streaming_utils.py
+++ b/letta/streaming_utils.py
@@ -264,39 +264,100 @@ class FunctionArgumentsStreamHandler:
def process_json_chunk(self, chunk: str) -> Optional[str]:
"""Process a chunk from the function arguments and return the plaintext version"""
- # Use strip to handle only leading and trailing whitespace in control structures
- if self.accumulating:
- clean_chunk = chunk.strip()
- if self.json_key in self.key_buffer:
- if ":" in clean_chunk:
- self.in_message = True
- self.accumulating = False
- return None
+ clean_chunk = chunk.strip()
+ # Not in message yet: accumulate until we see '<json_key>': (robust to split fragments)
+ if not self.in_message:
+ if clean_chunk == "{":
+ self.key_buffer = ""
+ self.accumulating = True
+ return None
self.key_buffer += clean_chunk
+ if self.json_key in self.key_buffer and ":" in clean_chunk:
+ # Enter value mode; attempt to extract inline content if it exists in this same chunk
+ self.in_message = True
+ self.accumulating = False
+ # Try to find the first quote after the colon within the original (unstripped) chunk
+ s = chunk
+ colon_idx = s.find(":")
+ if colon_idx != -1:
+ q_idx = s.find('"', colon_idx + 1)
+ if q_idx != -1:
+ self.message_started = True
+ rem = s[q_idx + 1 :]
+ # Check if this same chunk also contains the terminating quote (and optional delimiter)
+ j = len(rem) - 1
+ while j >= 0 and rem[j] in " \t\r\n":
+ j -= 1
+ if j >= 1 and rem[j - 1] == '"' and rem[j] in ",}]":
+ out = rem[: j - 1]
+ self.in_message = False
+ self.message_started = False
+ return out
+ if j >= 0 and rem[j] == '"':
+ out = rem[:j]
+ self.in_message = False
+ self.message_started = False
+ return out
+ # No terminator yet; emit remainder as content
+ return rem
+ return None
+ if clean_chunk == "}":
+ self.in_message = False
+ self.message_started = False
+ self.key_buffer = ""
return None
+ # Inside message value
if self.in_message:
- if chunk.strip() == '"' and self.message_started:
+ # Bare opening/closing quote tokens
+ if clean_chunk == '"' and self.message_started:
self.in_message = False
self.message_started = False
return None
- if not self.message_started and chunk.strip() == '"':
+ if not self.message_started and clean_chunk == '"':
self.message_started = True
return None
if self.message_started:
- if chunk.strip().endswith('"'):
+ # Detect closing patterns: '"', '",', '"}' (with optional whitespace)
+ i = len(chunk) - 1
+ while i >= 0 and chunk[i] in " \t\r\n":
+ i -= 1
+ if i >= 1 and chunk[i - 1] == '"' and chunk[i] in ",}]":
+ out = chunk[: i - 1]
self.in_message = False
- return chunk.rstrip('"\n')
+ self.message_started = False
+ return out
+ if i >= 0 and chunk[i] == '"':
+ out = chunk[:i]
+ self.in_message = False
+ self.message_started = False
+ return out
+ # Otherwise, still mid-string
return chunk
- if chunk.strip() == "{":
- self.key_buffer = ""
- self.accumulating = True
- return None
-
- if chunk.strip() == "}":
+ if clean_chunk == "}":
self.in_message = False
self.message_started = False
+ self.key_buffer = ""
return None
return None
+
+
+def sanitize_streamed_message_content(text: str) -> str:
+ """Remove trailing JSON delimiters that can leak into assistant text.
+
+ Specifically handles cases where a message string is immediately followed
+ by a JSON delimiter in the stream (e.g., '"', '",', '"}', '" ]').
+ Internal commas inside the message are preserved.
+ """
+ if not text:
+ return text
+ t = text.rstrip()
+ # strip trailing quote + delimiter
+ if len(t) >= 2 and t[-2] == '"' and t[-1] in ",}]":
+ return t[:-2]
+ # strip lone trailing quote
+ if t.endswith('"'):
+ return t[:-1]
+ return t