1010 IStreamProcessor ,
1111 StreamingContent ,
1212)
13- from src .core .services .json_repair_service import JsonRepairService
13+ from src .core .services .json_repair_service import JsonRepairResult , JsonRepairService
1414
1515logger = logging .getLogger (__name__ )
1616
@@ -67,9 +67,9 @@ async def process(self, content: StreamingContent) -> StreamingContent:
6767 else :
6868 i = self ._process_json_character (text , i )
6969 if self ._is_json_complete ():
70- repaired_json , success = self ._handle_json_completion ()
71- if success :
72- out_parts .append (json .dumps (repaired_json ))
70+ repair_result = self ._handle_json_completion ()
71+ if repair_result . success :
72+ out_parts .append (json .dumps (repair_result . content ))
7373 else :
7474 out_parts .append (self ._buffer )
7575 self ._reset_state ()
@@ -151,26 +151,23 @@ def _is_current_quote_escaped(self) -> bool:
151151 def _is_json_complete (self ) -> bool :
152152 return self ._json_started and self ._brace_level == 0 and not self ._in_string
153153
154- def _handle_json_completion (self ) -> tuple [Any , bool ]:
155- repaired = None
156- success = False
154+ def _handle_json_completion (self ) -> JsonRepairResult :
157155 try :
158- repaired = self ._service .repair_and_validate_json (
156+ result = self ._service .repair_and_validate_json (
159157 self ._buffer ,
160158 schema = self ._schema ,
161159 strict = self ._strict_mode ,
162160 )
163- if repaired is not None :
164- success = True
165161 except Exception as e : # pragma: no cover - strict mode rethrow
166162 if self ._strict_mode :
167163 raise JSONParsingError (
168164 message = f"JSON repair failed in strict mode: { e } " ,
169165 details = {"original_buffer" : self ._buffer },
170166 ) from e
171167 logger .warning ("JSON repair raised error: %s" , e )
168+ return JsonRepairResult (success = False , content = None )
172169
173- if repaired is not None :
170+ if result . success :
174171 metrics .inc (
175172 "json_repair.streaming.strict_success"
176173 if self ._strict_mode
@@ -185,7 +182,7 @@ def _handle_json_completion(self) -> tuple[Any, bool]:
185182 logger .warning (
186183 "JSON block detected but failed to repair. Flushing raw buffer."
187184 )
188- return repaired , success
185+ return result
189186
190187 def _log_buffer_capacity_warning (self ) -> None :
191188 if self ._json_started and len (self ._buffer ) > self ._buffer_cap_bytes :
@@ -199,16 +196,16 @@ def _flush_final_buffer(self) -> str | None:
199196 if not self ._in_string and buf .rstrip ().endswith (":" ):
200197 buf = buf + " null"
201198 self ._buffer = buf
202- repaired_final = self ._service .repair_and_validate_json (
199+ repair_result = self ._service .repair_and_validate_json (
203200 buf , schema = self ._schema , strict = self ._strict_mode
204201 )
205- if repaired_final is not None :
202+ if repair_result . success :
206203 metrics .inc (
207204 "json_repair.streaming.strict_success"
208205 if self ._strict_mode
209206 else "json_repair.streaming.best_effort_success"
210207 )
211- return json .dumps (repaired_final )
208+ return json .dumps (repair_result . content )
212209 else :
213210 metrics .inc (
214211 "json_repair.streaming.strict_fail"
0 commit comments