Skip to content

add Ollama health check endpoint#197

Merged
ionfwsrijan merged 2 commits into
ionfwsrijan:mainfrom
lakshay122007:feat/6.1-ollama-health-check
Jun 26, 2026
Merged

add Ollama health check endpoint#197
ionfwsrijan merged 2 commits into
ionfwsrijan:mainfrom
lakshay122007:feat/6.1-ollama-health-check

Conversation

@lakshay122007

Copy link
Copy Markdown
Contributor

Before opening: make sure there is an issue tracking this work, and link it below. PRs without a linked issue may be closed without review.

Linked issue

Closes #196

What this PR does

This PR introduces a dedicated health check endpoint (GET /api/health/ollama) to monitor the local Ollama LLM service. It safely wraps the httpx request to gracefully catch connection refusals and timeouts, ensuring the backend returns a clean available: false JSON payload instead of crashing with a 500 Internal Server Error.

Type of change

  • Bug fix
  • New feature
  • ML model / training pipeline
  • Refactor (no behaviour change)
  • Documentation
  • Tests only

ML tier (if applicable)

  • Tier 1 — Triage
  • Tier 2 — Predictive
  • Tier 3 — Autonomous
  • Not ML-related

Stack affected

  • Backend
  • Frontend
  • Both

Changes

Backend

  • Added GET /api/health/ollama route in app/main.py with custom exception handling for timeouts/refusals.
  • Created tests/test_health.py utilizing AsyncMock to simulate httpx.AsyncClient context managers for complete test coverage of both online and offline states.

Frontend

New dependencies

Database / schema changes


Testing

How did you test this?

Executed the pytest suite (python -m pytest tests/test_health.py --cov=app) which validates successful JSON parsing of model tags, as well as fallback behavior for httpx.ConnectError and 404/500 status codes. All tests pass with 100% green coverage.

Checklist

  • Tested locally end-to-end (upload ZIP or GitHub URL → scan → findings returned correctly)
  • New ML model falls back gracefully when model file is absent
  • No new console.error or unhandled Python exceptions introduced
  • Added or updated tests where applicable
  • requirements.txt / package.json updated if new dependencies added
  • New model files (.pkl, .pt, etc.) are gitignored, not committed

Anything reviewers should focus on

The test file uses a custom create_mock_client helper to properly mock the async context manager (async with httpx.AsyncClient()) so that FastAPI resolves the coroutine safely in the test client.

Screenshots (if UI changed)

@github-actions

Copy link
Copy Markdown

🎉 Thank you @lakshay122007 for submitting a Pull Request!

We're excited to review your contribution.

Before Review

✅ Ensure all CI checks pass
✅ Complete the PR template
✅ Link the related issue

Want faster reviews and contributor support?

Join our Discord community:

🔗 https://discord.gg/FcXuyw2Rs

Maintainers and mentors are active there and can help resolve blockers quickly.

Happy Contributing! 🚀

@github-actions github-actions Bot added backend Backend issues feature New feature ml ML related issues SSoC26 labels Jun 25, 2026
@github-actions

Copy link
Copy Markdown

PR template check passed!

@arpit2006 this PR is ready for your review. 🚀

@lakshay122007

Copy link
Copy Markdown
Contributor Author

hi @ionfwsrijan @arpit2006 Please review it. Thanks!

@arpit2006 arpit2006 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approve with 2 minor (non-blocking) notes

The feature is correct, safe, and well-tested. Here's the full breakdown:


✅ What's Good

Area Detail
Core goal met Endpoint never returns 500 on connection refusal — exactly what the linked issue required
async with usage httpx.AsyncClient is correctly used as an async context manager, no resource leaks
Response schema All three code paths (success, non-200, exception) return the same { available, models, base_url } shape
Test mock correctness create_mock_client properly mocks __aenter__/__aexit__ — the correct way to mock an async context manager, a common gotcha
Test coverage All 3 branches covered: online ✅, ConnectError ✅, non-200 status ✅
No new dependency httpx was already in requirements.txt

⚠️ Two Minor Notes (Non-Blocking)

1. Overly-broad exception handler

# Current — catches everything including AttributeError, KeyError, etc.
except Exception as e:

Genuine bugs inside the try block (e.g. a bad .json() parse) get silently swallowed. Recommend narrowing to:

except (httpx.ConnectError, httpx.TimeoutException, httpx.RequestError) as e:

A corresponding test_ollama_health_timeout test case is also missing.

2. Hardcoded base_url

base_url = "http://localhost:11434"  # not configurable

Users on Docker (http://ollama:11434) or a remote host can't configure this without a code change. A one-liner fix:

OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434")

These are quality-of-life improvements, not blockers. The PR achieves its stated goal cleanly. Safe to approve — the two notes above can be addressed in follow-up issues if the maintainer agrees.

LGTM!

The remaining changes are Non - Blocking can be addressed in future refactor!

@ionfwsrijan , Ready to Merge!

@arpit2006

Copy link
Copy Markdown
Collaborator

@lakshay122007 , Approved!

@github-actions

Copy link
Copy Markdown

PR template check passed!

@arpit2006 this PR is ready for your review. 🚀

@lakshay122007

Copy link
Copy Markdown
Contributor Author

hi @arpit2006 Made the minor changes as you mentioned..

@ionfwsrijan ionfwsrijan merged commit 60c46bb into ionfwsrijan:main Jun 26, 2026
11 checks passed
@ionfwsrijan ionfwsrijan added the Medium Medium difficulty label Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add Ollama health check endpoint

3 participants