@@ -7399,3 +7399,167 @@ def test_parse_base_json_reports_the_name_as_emitted(sample_tools):
73997399
74007400 assert [(c .name , c .tool_index )
74017401 for c in calls ] == [("functions.get_weather" , - 1 )]
7402+
7403+
7404+ # ============================================================================
7405+ # DeepSeek DSML layout: the newlines inside a tool-call block are not content
7406+ # ============================================================================
7407+
7408+ _DSML_FORMATS = [
7409+ (DeepSeekV32Parser , "function_calls" , "invoke" , "parameter" ),
7410+ (DeepSeekV4Parser , "tool_calls" , "invoke" , "parameter" ),
7411+ (DeepSeekV41Parser , " calls" , " invoke" , " parameter" ),
7412+ ]
7413+
7414+
7415+ def _dsml_block (calls_tag : str , invoke_tag : str , parameter_tag : str ) -> str :
7416+ """Two calls laid out as the checkpoint encoders write them: one per line."""
7417+ dsml = "|DSML|"
7418+
7419+ def invoke (name : str , key : str , value : str ) -> str :
7420+ return (f'<{ dsml } { invoke_tag } name="{ name } ">\n '
7421+ f'<{ dsml } { parameter_tag } name="{ key } " string="true">{ value } '
7422+ f"</{ dsml } { parameter_tag } >\n "
7423+ f"</{ dsml } { invoke_tag } >" )
7424+
7425+ return (f"<{ dsml } { calls_tag } >\n " +
7426+ invoke ("get_weather" , "location" , "NYC" ) + "\n " +
7427+ invoke ("search_web" , "query" , "AI" ) + "\n " +
7428+ f"</{ dsml } { calls_tag } >" )
7429+
7430+
7431+ def _stream (parser ,
7432+ text : str ,
7433+ chunk_size : int ,
7434+ tools : list [ChatCompletionToolsParam ],
7435+ * ,
7436+ finish : bool = True ):
7437+ results = [
7438+ parser .parse_streaming_increment (text [i :i + chunk_size ], tools )
7439+ for i in range (0 , len (text ), chunk_size )
7440+ ]
7441+ if finish :
7442+ results .append (parser .finish (tools ))
7443+ content = "" .join (result .normal_text for result in results )
7444+ calls = [call for result in results for call in result .calls ]
7445+ return content , calls
7446+
7447+
7448+ @pytest .mark .parametrize ("parser_cls, calls_tag, invoke_tag, parameter_tag" ,
7449+ _DSML_FORMATS )
7450+ @pytest .mark .parametrize ("chunk_size" , [1 , 3 , 7 , 10_000 ])
7451+ def test_dsml_newlines_between_calls_are_not_content (
7452+ sample_tools : list [ChatCompletionToolsParam ],
7453+ parser_cls : type [BaseToolParser ], calls_tag : str , invoke_tag : str ,
7454+ parameter_tag : str , chunk_size : int ) -> None :
7455+ """The newline after each invoke is the block's layout, however it is chunked.
7456+
7457+ Streamed as text, it became content after every tool call: over Anthropic
7458+ Messages, a whitespace-only text block followed each tool_use, and a client
7459+ that drops such blocks sent back a turn that no longer matched what was
7460+ served.
7461+ """
7462+ text = "Let me check.\n \n " + _dsml_block (calls_tag , invoke_tag ,
7463+ parameter_tag )
7464+
7465+ content , calls = _stream (parser_cls (), text , chunk_size , sample_tools )
7466+
7467+ assert content == "Let me check."
7468+ assert [call .name for call in calls
7469+ if call .name ] == ["get_weather" , "search_web" ]
7470+ arguments = [
7471+ json .loads (call .parameters ) for call in calls
7472+ if not call .name and call .parameters
7473+ ]
7474+ assert arguments == [{"location" : "NYC" }, {"query" : "AI" }]
7475+
7476+
7477+ @pytest .mark .parametrize ("parser_cls, calls_tag, invoke_tag, parameter_tag" ,
7478+ _DSML_FORMATS )
7479+ @pytest .mark .parametrize ("chunk_size" , [5 , 10_000 ])
7480+ def test_dsml_text_after_the_block_is_still_content (
7481+ sample_tools : list [ChatCompletionToolsParam ],
7482+ parser_cls : type [BaseToolParser ], calls_tag : str , invoke_tag : str ,
7483+ parameter_tag : str , chunk_size : int ) -> None :
7484+ """Only the layout inside the block is dropped; text after it is kept.
7485+
7486+ In one increment, the closing tag and the text after it follow the last
7487+ invoke; that text must come out of that increment.
7488+ """
7489+ text = _dsml_block (calls_tag , invoke_tag , parameter_tag ) + " Done."
7490+
7491+ parser = parser_cls ()
7492+ content , calls = _stream (parser ,
7493+ text ,
7494+ chunk_size ,
7495+ sample_tools ,
7496+ finish = False )
7497+
7498+ assert content == " Done."
7499+ finish_result = parser .finish (sample_tools )
7500+ assert finish_result .normal_text == ""
7501+ assert finish_result .calls == []
7502+ assert [call .name for call in calls
7503+ if call .name ] == ["get_weather" , "search_web" ]
7504+
7505+
7506+ @pytest .mark .parametrize ("parser_cls, calls_tag, invoke_tag, parameter_tag" ,
7507+ _DSML_FORMATS )
7508+ @pytest .mark .parametrize ("chunk_size" , [1 , 2 , 3 , 7 , 10_000 ])
7509+ def test_dsml_blank_line_before_the_block_is_not_content (
7510+ sample_tools : list [ChatCompletionToolsParam ],
7511+ parser_cls : type [BaseToolParser ], calls_tag : str , invoke_tag : str ,
7512+ parameter_tag : str , chunk_size : int ) -> None :
7513+ """The encoders write a blank line between the content and the block.
7514+
7515+ The content keeps its own whitespace; only that blank line is dropped, on
7516+ both paths.
7517+ """
7518+ content_in = " Indented first line.\n Last line.\n "
7519+ text = content_in + "\n \n " + _dsml_block (calls_tag , invoke_tag ,
7520+ parameter_tag )
7521+
7522+ content , calls = _stream (parser_cls (), text , chunk_size , sample_tools )
7523+
7524+ assert content == content_in
7525+ assert parser_cls ().detect_and_parse (text ,
7526+ sample_tools ).normal_text == content_in
7527+ assert [call .name for call in calls
7528+ if call .name ] == ["get_weather" , "search_web" ]
7529+
7530+
7531+ @pytest .mark .parametrize ("parser_cls, calls_tag, invoke_tag, parameter_tag" ,
7532+ _DSML_FORMATS )
7533+ @pytest .mark .parametrize ("chunk_size" , [1 , 3 , 10_000 ])
7534+ def test_dsml_trailing_blank_lines_without_a_block_are_content (
7535+ sample_tools : list [ChatCompletionToolsParam ],
7536+ parser_cls : type [BaseToolParser ], calls_tag : str , invoke_tag : str ,
7537+ parameter_tag : str , chunk_size : int ) -> None :
7538+ """Newlines held back in case a block follows come out when none does."""
7539+ text = "Done.\n \n "
7540+
7541+ content , calls = _stream (parser_cls (), text , chunk_size , sample_tools )
7542+
7543+ assert content == text
7544+ assert calls == []
7545+
7546+
7547+ @pytest .mark .parametrize ("parser_cls, calls_tag, invoke_tag, parameter_tag" ,
7548+ _DSML_FORMATS )
7549+ @pytest .mark .parametrize ("chunk_size" , [1 , 3 , 10_000 ])
7550+ def test_dsml_stream_cut_inside_the_block_keeps_only_the_content (
7551+ sample_tools : list [ChatCompletionToolsParam ],
7552+ parser_cls : type [BaseToolParser ], calls_tag : str , invoke_tag : str ,
7553+ parameter_tag : str , chunk_size : int ) -> None :
7554+ """A stream cut short inside the block ends with its content.
7555+
7556+ The non-streaming path reads the same text the same way.
7557+ """
7558+ block = _dsml_block (calls_tag , invoke_tag , parameter_tag )
7559+ text = "Checking.\n \n " + block [:block .index ("</|DSML|" )]
7560+
7561+ content , _ = _stream (parser_cls (), text , chunk_size , sample_tools )
7562+
7563+ assert content == "Checking."
7564+ assert parser_cls ().detect_and_parse (text ,
7565+ sample_tools ).normal_text == content
0 commit comments