Skip to content

feat(ai-ollama): enhance error handling in chatStream to emit RUN_ERROR events#862

Open
harshlocham wants to merge 1 commit into
TanStack:mainfrom
harshlocham:fix/ollama-prestream-run-error
Open

feat(ai-ollama): enhance error handling in chatStream to emit RUN_ERROR events#862
harshlocham wants to merge 1 commit into
TanStack:mainfrom
harshlocham:fix/ollama-prestream-run-error

Conversation

@harshlocham

@harshlocham harshlocham commented Jun 28, 2026

Copy link
Copy Markdown
Contributor
  • Added error handling in the OllamaTextAdapter's chatStream method to yield RUN_ERROR events instead of throwing errors directly.
  • Introduced utility functions to format error payloads and raw events for better error reporting.
  • Added a new test suite to verify the correct emission of RUN_ERROR events on chat stream rejections.

🎯 Changes

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling in Ollama chat streaming so failures now return a structured error event instead of throwing.
    • Included clearer error details, such as message, code, and available raw event data, to help surface stream failures consistently.
  • Tests
    • Added coverage for rejected chat requests to confirm a single error event is emitted and no exception is thrown.

…OR events

- Added error handling in the OllamaTextAdapter's chatStream method to yield RUN_ERROR events instead of throwing errors directly.
- Introduced utility functions to format error payloads and raw events for better error reporting.
- Added a new test suite to verify the correct emission of RUN_ERROR events on chat stream rejections.
@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

OllamaTextAdapter.chatStream now imports toRunErrorPayload and toRunErrorRawEvent from @tanstack/ai/adapter-internals. The catch block no longer rethrows; it builds a structured RUN_ERROR chunk and yields it to the stream. A new test verifies this behavior on upstream rejection.

Ollama chatStream RUN_ERROR emission

Layer / File(s) Summary
chatStream error handling and test
packages/ai-ollama/src/adapters/text.ts, packages/ai-ollama/tests/text-adapter.test.ts
Imports toRunErrorPayload/toRunErrorRawEvent; replaces rethrow with a RUN_ERROR chunk yield containing message, code, optional rawEvent, and an error object. Test mocks a pre-stream rejection and asserts the single emitted chunk type and message.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • TanStack/ai#673: Applies the same toRunErrorRawEvent/RUN_ERROR enrichment pattern that this PR now adds to the Ollama adapter's error path.

Suggested reviewers

  • AlemTuzlak

🐇 A stream once crashed and threw a fit,
now errors land soft, yielded bit by bit.
RUN_ERROR hops along with grace,
rawEvent tucked in its little place.
No more rethrows — just a tidy chunk! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is specific and accurately summarizes the main change: Ollama chatStream now emits RUN_ERROR events on failures.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description matches the required template and includes the change summary, checklist, and release impact.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant