An earlier blog post explained how to access the Ollama API from the shell using the curl program. If the gemma3 model is used as a local AI on the system, it might look like this:
curl http://localhost:11434/api/generate -d '{
"model": "gemma3",
"prompt": "Who was Ernst Haas?",
"stream": false
}'In this article, we’ll take a look at the Python programming language and how to use the requests library. The Ollama API is also accessed, with the endpoint being identical to the one in the curl example.
http://localhost:11434/api/generateFor this example, I am using qwen2.5-coder as the model. If you use a different language model, you will need to adjust the code accordingly.
The request is transmitted to the language model in JSON format. Accordingly, the json library must be imported in the Python script. We also need requests („HTTP for Humans“), a library used to send HTTP requests. You can find detailed documentation on the Requests project website. Finally, since I want to handle potential connection errors, I have also included RequestException. The first lines of the Python script therefore look like this:
#!/usr/bin/env python3
import json
import requests
from requests.exceptions import RequestExceptionAs with the curl command, the endpoint must be specified:
URL = "http://localhost:11434/api/generate"This refers to the HTTP endpoint of the locally running Ollama service, where api/generate is the API route for text generation. Next, let’s move on to the data to be submitted (in JSON format):
data = {
"model": "qwen2.5-coder",
"prompt": "Create a list of 5 programming languages in Python and assign it to the variable `languages`.",
"max_tokens": 1000,
"stream": False,
}This is very similar to the data transmitted using the curl command. In this example, however, a limit on the number of tokens has been added ("max_tokens": 1000).
The actual work is now performed by the function fetch_generated_text():
def fetch_generated_text() -> str:
"""
Fetch generated text from the Ollama API.
"""
try:
# Make a POST request to the Ollama API
response: requests.Response = requests.post(URL, json=data)
response.raise_for_status() # Raise an exception for HTTP errors
except RequestException as e:
sys.exit(f"No connection to Ollama API or HTTP error.\nDetails: {e}")
try:
# Parse the JSON response
result = response.json()
except json.JSONDecodeError as e:
sys.exit(f"Failed to parse JSON response.\nDetails: {e}")
generated_text = result.get("response")
# Check if the generated text is a valid string
if not isinstance(generated_text, str):
sys.exit("The response does not contain a valid 'response' text.")
return generated_textIn this example, the request to the Ollama API is wrapped in a try–except block. This exception handling ensures that the program terminates by calling sys.exit() in the event of a connection error. This occurs, for instance, if Ollama is not running in the background on the machine—meaning it has not been started.
Next, an attempt is made to parse the received JSON data. A try–except block is used here as well. If parsing fails, sys.exit() is called and the script terminates.
Finally, a check is performed to see whether a valid string was returned; sys.exit() is called if this is not the case.
In the main() function, fetch_generated_text() is called and the response is output to the terminal:
def main() -> None:
answer: str = fetch_generated_text()
print(answer, end="", flush=True)
if __name__ == "__main__":
main()You could also save the answer in a Markdown document by adding the following code to main():
with open("generated_output.md", "a", encoding="utf-8") as md_file:
md_file.write(answer)The fetch_generated_text() function could also be implemented differently. If you look online for examples of how to use requests, you will find other implementations. Simply regard the version chosen here as a suggestion for your own script.
Finally, I’ll show the complete code so you can copy it and use it on your computer.
import json
import sys
import requests
from requests.exceptions import RequestException
URL = "http://localhost:11434/api/generate"
data = {
"model": "qwen2.5-coder",
"prompt": "Create a list of 5 programming languages in Python and assign it to the variable `languages`.",
"max_tokens": 1000,
"stream": False,
}
def fetch_generated_text() -> str:
"""
Fetch generated text from the Ollama API.
"""
try:
# Make a POST request to the Ollama API
response: requests.Response = requests.post(URL, json=data)
response.raise_for_status() # Raise an exception for HTTP errors
except RequestException as e:
sys.exit(f"No connection to Ollama API or HTTP error.\nDetails: {e}")
try:
# Parse the JSON response
result = response.json()
except json.JSONDecodeError as e:
sys.exit(f"Failed to parse JSON response.\nDetails: {e}")
generated_text = result.get("response")
# Check if the generated text is a valid string
if not isinstance(generated_text, str):
sys.exit("The response does not contain a valid 'response' text.")
return generated_text
def main() -> None:
answer: str = fetch_generated_text()
print(answer, end="", flush=True)
if __name__ == "__main__":
main()