ollama generators retry a 400 Bad Request forever, so a scan never finishes

Open Beginner friendly
#2,579 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ollama, python
Domain
backend

Research direction

Start with OllamaGenerator._call_model and OllamaGeneratorChat._call_model in the Ollama generator code, then read the existing 404 tests in tests/generators/test_ollama.py. Add mocked response coverage for the described 400, 401, 429 and 503 behavior. Done means rejected requests follow the proposed handling while transient errors still back off.

Written by the indexing model from the issue text.

Description

needs-triage

Steps to reproduce

Point the Ollama generator at a model that can't chat. An embedding model is enough, and needs nothing except a local Ollama:

ollama pull nomic-embed-text
python -m garak --target_type ollama --target_name nomic-embed-text \
  --probes test.Blank --detectors always.Pass

Ollama answers every request with 400 "nomic-embed-text" does not support chat. The run never finishes and the terminal shows no error. garak.log keeps adding:

INFO  HTTP Request: POST http://127.0.0.1:11434/api/chat "HTTP/1.1 400 Bad Request"
INFO  Backing off _call_model(...) for 57.9s (garak.exception.GeneratorBackoffTrigger)

I stopped it after 150 s. By then it had retried 12 times, with the wait growing towards the 70 s cap.

I first hit this scanning a RAG server through its Ollama-compatible endpoint (LightRAG). That server returns 400 only for prompts it won't accept, such as ones that are too short. test.Test sends "" and "\\", so the scan stalled on its first prompt and never got to the rest.

Expected behavior

A 400 means the server rejected the request, and sending it again gets the same answer. The other generators already treat it that way:

  • openai: BadRequestError is logged and the prompt gets [None]; 401/403 raise GarakException; only transient_retry_codes back off.
  • anthropic: only rate limit, connection, timeout and 5xx errors back off. The comment there says the other 4xx "would loop indefinitely if retried".
  • rest: a 4xx raises ConnectionError, which is outside its backoff. litellm raises BadGeneratorException on BadRequestError.

Current behavior

OllamaGenerator._call_model and OllamaGeneratorChat._call_model turn every ollama.ResponseError except 404 into GeneratorBackoffTrigger. The backoff.on_exception around them has no max_tries or max_time, so a 400, 401 or 403 is retried forever.

I read #2141 and the "garak is tenacious" reasoning. I'm not asking for a retry limit, and transient errors should keep retrying. This is only about the server saying the request itself is wrong.

Proposed fix

Keep backing off on timeouts, 408, 429 and 5xx. For the rest, follow openai.py:

  • 400: log it and return [None] for that prompt, so one rejected prompt doesn't block the scan.
  • 401 / 403: raise GarakException with a hint to check OLLAMA_API_KEY.
  • 404 and other 4xx: raise, as 404 does today.

Tests would be respx mocks next to the existing 404 tests in tests/generators/test_ollama.py: a 400 returns [None] after a single request, a 401 raises, and 429 and 503 still back off. I'm happy to send the PR if this direction works for you.

garak version

main @ 67124f8d (v0.17.2.pre1)

Additional Information

  1. macOS
  2. Python 3.12
  3. Repository checkout, uv pip install -e .
  4. ollama Python client 0.6.3, local Ollama with nomic-embed-text pulled
  5. Command line flags only, as above
Dominant language
Python
Stars
10.2k
Forks
1.5k
Avg merge
9d 20h
Merged PRs (30d)
15

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NVIDIA/garak

All issues in NVIDIA/garak

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.