A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
strip first and last chunks for backtick checking (#7510)
Shahmir Varqha44a5859336d5
diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.pyindex 1795fda147..111969135b 100644--- a/marimo/_server/ai/providers.py+++ b/marimo/_server/ai/providers.py@@ -1296,22 +1296,22 @@ async def without_wrapping_backticks(# Handle the first chunkif first_chunk:first_chunk = False+ stripped_chunk = chunk.lstrip()# Check for language-specific fences firstfor lang in langs:- if chunk.startswith(f"```{lang}"):+ if stripped_chunk.startswith(f"```{lang}"):has_starting_backticks = True- chunk = chunk[- 3 + len(lang) :- ] # Remove the starting backticks with lang+ # Remove the starting backticks with lang+ chunk = stripped_chunk[3 + len(lang) :]# Also remove starting newline if presentif chunk.startswith("\n"):chunk = chunk[1:]break# If no language-specific fence was found, check for plain backtickselse:- if chunk.startswith("```"):+ if stripped_chunk.startswith("```"):has_starting_backticks = True- chunk = chunk[3:] # Remove the starting backticks+ chunk = stripped_chunk[3:] # Remove the starting backticks# Also remove starting newline if presentif chunk.startswith("\n"):chunk = chunk[1:]@@ -1325,9 +1325,13 @@ async def without_wrapping_backticks(# Handle the last chunkif buffer is not None:+ # Some models add trailing space to the end of the response, so we strip to check for backticks+ stripped_buffer = buffer.rstrip()+ trailing_space = buffer[len(stripped_buffer) :]+# Remove ending newline if present- if buffer.endswith("\n```"):- buffer = buffer[:-4] # Remove the ending newline and backticks- elif has_starting_backticks and buffer.endswith("```"):- buffer = buffer[:-3] # Remove just the ending backticks+ if stripped_buffer.endswith("\n```"):+ buffer = stripped_buffer[:-4] + trailing_space+ elif has_starting_backticks and stripped_buffer.endswith("```"):+ buffer = stripped_buffer[:-3] + trailing_spaceyield bufferdiff --git a/tests/_server/api/endpoints/test_ai.py b/tests/_server/api/endpoints/test_ai.pyindex 9783e97565..0009a09da6 100644--- a/tests/_server/api/endpoints/test_ai.py+++ b/tests/_server/api/endpoints/test_ai.py@@ -1027,6 +1027,41 @@ class TestGetFinishReason(unittest.TestCase):["```sql\n", "SELECT * FROM table\n", "WHERE id = 1\n```"],"SELECT * FROM table\nWHERE id = 1",),+ # Test trailing whitespace preservation after closing backticks+ (+ ["```", "print('hello')", "``` "],+ "print('hello') ",+ ),+ (+ ["```python\n", "print('hello')", "\n``` "],+ "print('hello') ",+ ),+ (+ ["```", "code", "```\t\n"],+ "code\t\n",+ ),+ # Test leading whitespace before opening backticks (whitespace and backticks stripped)+ (+ [" ```", "code", "```"],+ "code",+ ),+ (+ [" ```python\n", "code", "```"],+ "code",+ ),+ (+ ["\t```", "code", "```"],+ "code",+ ),+ # Test opening backticks with extra characters after language (language stripped, rest preserved)+ (+ ["```python ", "code", "```"],+ " code",+ ),+ (+ ["```python\t", "code", "```"],+ "\tcode",+ ),],)async def test_without_wrapping_backticks(