Skip to content

Commit cf72fd9

Browse files
Kasper JungeRalphify
authored andcommitted
refactor: replace parsed = None sentinel with try/except/else in _read_agent_stream
The sentinel was a no-op placeholder for the error path: setting `parsed = None` just so `isinstance(parsed, dict)` would skip the forwarding block on the next line. Using the `try/except/else` structure expresses the same intent directly — the dict-handling logic runs only when `json.loads` succeeds — and drops the sentinel assignment plus the redundant dict check on the JSON-error path. Behavior preserved: the inner `isinstance(parsed, dict)` still gates forwarding for valid-but-non-dict JSON (lists, strings, numbers). Covered by the existing stream tests (`test_agent.py::test_stream_json_*`). Co-authored-by: Ralphify <noreply@ralphify.co>
1 parent 1751d89 commit cf72fd9

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/ralphify/_agent.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,13 +383,14 @@ def _read_agent_stream(
383383
try:
384384
parsed = json.loads(stripped)
385385
except json.JSONDecodeError:
386-
parsed = None
387-
if isinstance(parsed, dict):
388-
if parsed.get("type") == _RESULT_EVENT_TYPE and isinstance(
389-
parsed.get(_RESULT_FIELD), str
390-
):
391-
result_text = parsed[_RESULT_FIELD]
392-
_call_safely(on_activity, parsed)
386+
pass
387+
else:
388+
if isinstance(parsed, dict):
389+
if parsed.get("type") == _RESULT_EVENT_TYPE and isinstance(
390+
parsed.get(_RESULT_FIELD), str
391+
):
392+
result_text = parsed[_RESULT_FIELD]
393+
_call_safely(on_activity, parsed)
393394

394395
# Also check deadline after processing — if the reader thread
395396
# already queued many lines, this prevents unbounded processing

0 commit comments

Comments
 (0)