[GH-ISSUE #9372] How to cancel a generate task by ollama restful api #68175

Closed
opened 2026-05-04 12:44:37 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @LovelyCatEx on GitHub (Feb 26, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/9372

How to cancel a generate task by ollama restful api?

Originally created by @LovelyCatEx on GitHub (Feb 26, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/9372 How to cancel a generate task by ollama restful api?
GiteaMirror added the feature request label 2026-05-04 12:44:37 -05:00
Author
Owner

@flywiththetide commented on GitHub (Mar 4, 2025):

As of now, Ollama does not provide a direct REST API endpoint for canceling an in-progress generate request. However, here are some workarounds you can try:

1. Use HTTP Connection Termination

Since Ollama’s /api/generate endpoint uses streaming responses, you can cancel a request by closing the connection:

  • If using curl, press Ctrl+C to stop it.
  • If using Python, call .close() on the request session.

2. Stop the Ollama Server (For Running Background Tasks)

If a model is running in the background, you can stop the server to forcefully halt all requests:

pkill -f ollama

Or if running inside a Docker container:

docker stop ollama-container

3. Process Management (Linux)

Find and kill the specific process:

ps aux | grep ollama
kill -9 <PID>

4. Expecting a Future API Update

A POST /api/cancel endpoint could be a great addition to Ollama in the future! You might want to suggest this as a feature request.

Would you like me to submit an official feature request for a /api/cancel endpoint?

<!-- gh-comment-id:2696232245 --> @flywiththetide commented on GitHub (Mar 4, 2025): As of now, Ollama does not provide a direct **REST API endpoint** for canceling an in-progress `generate` request. However, here are some workarounds you can try: ### **1. Use HTTP Connection Termination** Since Ollama’s `/api/generate` endpoint uses **streaming responses**, you can cancel a request by closing the connection: - If using `curl`, press `Ctrl+C` to stop it. - If using Python, call `.close()` on the request session. ### **2. Stop the Ollama Server (For Running Background Tasks)** If a model is running in the background, you can stop the server to forcefully halt all requests: ```bash pkill -f ollama ``` Or if running inside a Docker container: ```bash docker stop ollama-container ``` ### **3. Process Management (Linux)** Find and kill the specific process: ```bash ps aux | grep ollama kill -9 <PID> ``` ### **4. Expecting a Future API Update** A `POST /api/cancel` endpoint could be a great addition to Ollama in the future! You might want to suggest this as a feature request. Would you like me to submit an official feature request for a `/api/cancel` endpoint?
Author
Owner

@jmorganca commented on GitHub (Apr 7, 2025):

Hi there, closing the connection that opened the request will stop generation. If this doesn't work for your use case, let me know and we can re-open

<!-- gh-comment-id:2784121897 --> @jmorganca commented on GitHub (Apr 7, 2025): Hi there, closing the connection that opened the request will stop generation. If this doesn't work for your use case, let me know and we can re-open
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#68175