Skip to content

fix(llmchat): mark tool/parsing errors as recoverable in streaming - #3733

Merged
crivetimihai merged 4 commits into
IBM:mainfrom
kimsehwan96:fix/llmchat-recoverable-error-handling
Mar 21, 2026
Merged

crivetimihai merged 4 commits into
IBM:mainfrom
kimsehwan96:fix/llmchat-recoverable-error-handling

Conversation

@kimsehwan96

@kimsehwan96 kimsehwan96 commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

🔗 Related Issue

Close: #3763


📝 Summary

When a tool invocation fails or a response parsing error occurs during LLM Chat streaming, the frontend disconnects the session and asks the user to reconnect. This happens because chat_events() wraps all exceptions — including ConnectionError and TimeoutError — into a single RuntimeError, which the router always marks as recoverable: False.

This PR lets ConnectionError/TimeoutError propagate with their original type so the router's existing handlers catch them correctly. The remaining RuntimeErrors (tool failures, parsing errors, model issues) are now marked recoverable: True since the session is still valid.


🏷️ Type of Change

  • Bug fix
  • Feature / Enhancement
  • Documentation
  • Refactor
  • Chore (deps, CI, tooling)
  • Other (describe below)

🧪 Verification

Check Command Status
Lint suite make lint pass
Unit tests make test pass
Coverage ≥ 80% make coverage pass

✅ Checklist

  • Code formatted (make black isort pre-commit)
  • Tests added/updated for changes
  • Documentation updated (if applicable)
  • No secrets or credentials committed

📓 Notes (optional)

스크린샷 2026-03-19 오전 10 49 34

Before: any error during streaming → RuntimeError("Chat processing error: ...") → recoverable: False → session killed

스크린샷 2026-03-19 오후 1 36 21

After: ConnectionError/TimeoutError → re-raised as-is → caught by existing router handlers.
Everything else → RuntimeError → recoverable: True → error shown but session stays alive.

@crivetimihai crivetimihai added bug Something isn't working SHOULD P2: Important but not vital; high-value items that are not crucial for the immediate release api REST API Related item labels Mar 20, 2026
@crivetimihai crivetimihai added this to the Release 1.0.0 milestone Mar 20, 2026
@crivetimihai

Copy link
Copy Markdown
Member

Thanks @kimsehwan96. Clean fix — correctly distinguishes connection-level errors (non-recoverable) from tool/parsing errors (recoverable). The approach of letting ConnectionError/TimeoutError propagate directly while marking RuntimeError as recoverable: True is the right design. One note: no linked issue — consider filing one for traceability. No tests, but this streaming error-path behavior is difficult to unit test. LGTM.

kimsehwan96 and others added 4 commits March 21, 2026 21:43
ConnectionError and TimeoutError are now re-raised directly from
chat_events() so the router can handle them with their original
type. Remaining RuntimeErrors (tool failures, parsing errors, model
config issues) are marked recoverable so the frontend keeps the
session alive instead of forcing a reconnect.

Signed-off-by: kimsehwan96 <[email protected]>
…opagation tests

Verify that token_streamer sets the correct recoverable value for each
error type (RuntimeError→true, ConnectionError→false, TimeoutError→true,
generic→false).  Add service-level tests confirming ConnectionError and
TimeoutError propagate from chat_events() without being wrapped as
RuntimeError.

Signed-off-by: Mihai Criveti <[email protected]>
Flake8 DAR401 flagged the missing exception types in the Raises section
after the new except clause was added.

Signed-off-by: Mihai Criveti <[email protected]>
…RuntimeError

Introduce ChatProcessingError(RuntimeError) for tool/parsing/model
failures that leave the session intact.  The router now catches
ChatProcessingError as recoverable and plain RuntimeError (e.g.
"not initialized") as non-recoverable, preventing a race where a
concurrent disconnect could leave the frontend on a dead session.

Signed-off-by: Mihai Criveti <[email protected]>
@crivetimihai
crivetimihai force-pushed the fix/llmchat-recoverable-error-handling branch from df652c5 to be55779 Compare March 21, 2026 22:27
@crivetimihai

Copy link
Copy Markdown
Member

Thanks for this fix @kimsehwan96 — the core observation that ConnectionError/TimeoutError were being swallowed by the catch-all except Exception (making the router's typed handlers dead code) is spot-on.

I rebased onto main, added tests, and made one additional change on top of your work:

Changes made during review

  1. Rebased onto main — clean, no conflicts.

  2. Added differential test coverage (4365ffc):

    • 4 existing router tests now assert the correct recoverable flag value for each error type.
    • 2 new service-level tests verify ConnectionError and TimeoutError propagate from chat_events() without being wrapped as RuntimeError.
  3. Docstring fix (fdab540): added ConnectionError, TimeoutError, and ChatProcessingError to the Raises section of chat_events() to satisfy flake8 DAR401.

  4. Introduced ChatProcessingError(RuntimeError) (be55779): plain RuntimeError is still raised for genuinely broken state (e.g. "Chat service not initialized" at line 2658), which should remain non-recoverable. The new subclass is used for the catch-all wrapper so the router can distinguish:

    • ChatProcessingError → recoverable: True (tool/parsing/model error, session intact)
    • RuntimeError → recoverable: False (session broken, e.g. concurrent disconnect)

    This prevents a race where a concurrent /disconnect could set _initialized = False between the router's guard check and chat_events() entry, leaving the frontend on a dead session marked as alive.

All tests pass (255 passed, 1 skipped).

@crivetimihai crivetimihai left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — the fix correctly separates recoverable (tool/parsing/model) errors from non-recoverable (session/connection) errors in the streaming path. Added ChatProcessingError to close the race condition gap, plus full differential test coverage. Ready to merge.

@crivetimihai
crivetimihai merged commit 9420f67 into IBM:main Mar 21, 2026
1 check passed
SergioChan pushed a commit to SergioChan/mcp-context-forge that referenced this pull request Mar 22, 2026
…BM#3733)

* fix(llmchat): mark tool/parsing errors as recoverable in streaming

ConnectionError and TimeoutError are now re-raised directly from
chat_events() so the router can handle them with their original
type. Remaining RuntimeErrors (tool failures, parsing errors, model
config issues) are marked recoverable so the frontend keeps the
session alive instead of forcing a reconnect.

Signed-off-by: kimsehwan96 <[email protected]>

* test(llmchat): assert recoverable flag and add service-layer error propagation tests

Verify that token_streamer sets the correct recoverable value for each
error type (RuntimeError→true, ConnectionError→false, TimeoutError→true,
generic→false).  Add service-level tests confirming ConnectionError and
TimeoutError propagate from chat_events() without being wrapped as
RuntimeError.

Signed-off-by: Mihai Criveti <[email protected]>

* fix(llmchat): add ConnectionError/TimeoutError to chat_events docstring

Flake8 DAR401 flagged the missing exception types in the Raises section
after the new except clause was added.

Signed-off-by: Mihai Criveti <[email protected]>

* fix(llmchat): distinguish recoverable ChatProcessingError from fatal RuntimeError

Introduce ChatProcessingError(RuntimeError) for tool/parsing/model
failures that leave the session intact.  The router now catches
ChatProcessingError as recoverable and plain RuntimeError (e.g.
"not initialized") as non-recoverable, preventing a race where a
concurrent disconnect could leave the frontend on a dead session.

Signed-off-by: Mihai Criveti <[email protected]>

---------

Signed-off-by: kimsehwan96 <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
msureshkumar88 pushed a commit that referenced this pull request May 13, 2026
…3733)

* fix(llmchat): mark tool/parsing errors as recoverable in streaming

ConnectionError and TimeoutError are now re-raised directly from
chat_events() so the router can handle them with their original
type. Remaining RuntimeErrors (tool failures, parsing errors, model
config issues) are marked recoverable so the frontend keeps the
session alive instead of forcing a reconnect.

Signed-off-by: kimsehwan96 <[email protected]>

* test(llmchat): assert recoverable flag and add service-layer error propagation tests

Verify that token_streamer sets the correct recoverable value for each
error type (RuntimeError→true, ConnectionError→false, TimeoutError→true,
generic→false).  Add service-level tests confirming ConnectionError and
TimeoutError propagate from chat_events() without being wrapped as
RuntimeError.

Signed-off-by: Mihai Criveti <[email protected]>

* fix(llmchat): add ConnectionError/TimeoutError to chat_events docstring

Flake8 DAR401 flagged the missing exception types in the Raises section
after the new except clause was added.

Signed-off-by: Mihai Criveti <[email protected]>

* fix(llmchat): distinguish recoverable ChatProcessingError from fatal RuntimeError

Introduce ChatProcessingError(RuntimeError) for tool/parsing/model
failures that leave the session intact.  The router now catches
ChatProcessingError as recoverable and plain RuntimeError (e.g.
"not initialized") as non-recoverable, preventing a race where a
concurrent disconnect could leave the frontend on a dead session.

Signed-off-by: Mihai Criveti <[email protected]>

---------

Signed-off-by: kimsehwan96 <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
kamath-a pushed a commit to kamath-a/mcp-context-forge that referenced this pull request Jun 26, 2026
…BM#3733)

* fix(llmchat): mark tool/parsing errors as recoverable in streaming

ConnectionError and TimeoutError are now re-raised directly from
chat_events() so the router can handle them with their original
type. Remaining RuntimeErrors (tool failures, parsing errors, model
config issues) are marked recoverable so the frontend keeps the
session alive instead of forcing a reconnect.

Signed-off-by: kimsehwan96 <[email protected]>

* test(llmchat): assert recoverable flag and add service-layer error propagation tests

Verify that token_streamer sets the correct recoverable value for each
error type (RuntimeError→true, ConnectionError→false, TimeoutError→true,
generic→false).  Add service-level tests confirming ConnectionError and
TimeoutError propagate from chat_events() without being wrapped as
RuntimeError.

Signed-off-by: Mihai Criveti <[email protected]>

* fix(llmchat): add ConnectionError/TimeoutError to chat_events docstring

Flake8 DAR401 flagged the missing exception types in the Raises section
after the new except clause was added.

Signed-off-by: Mihai Criveti <[email protected]>

* fix(llmchat): distinguish recoverable ChatProcessingError from fatal RuntimeError

Introduce ChatProcessingError(RuntimeError) for tool/parsing/model
failures that leave the session intact.  The router now catches
ChatProcessingError as recoverable and plain RuntimeError (e.g.
"not initialized") as non-recoverable, preventing a race where a
concurrent disconnect could leave the frontend on a dead session.

Signed-off-by: Mihai Criveti <[email protected]>

---------

Signed-off-by: kimsehwan96 <[email protected]>
Signed-off-by: Mihai Criveti <[email protected]>
Co-authored-by: Mihai Criveti <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api REST API Related item bug Something isn't working SHOULD P2: Important but not vital; high-value items that are not crucial for the immediate release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: LLM Chat streaming errors kill session instead of allowing recovery

2 participants