[Python] How do I fix the write() argument must be str, not bytes error when using Ollama with...

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use Ollama with Python to run a model (llama2) for a project. When I call ollama.chat() and try to print the response, I get a the error:

    Ollama error: write() argument must be str, not bytes

    I am not sure whether this happens because of the Ollama API response format, or because I am handling the data incorrectly in Python. I already tried decoding the response with .decode("utf-8"), but that didn’t work.

    How can I correctly handle the Ollama response in Python so that it prints without this error?

    I tried calling ollama.chat() with a simple user prompt and then printing the result directly. I also tried using .decode("utf-8") on the response in case it was in bytes, but it didn’t fix the issue.

    What I expected: The model should return a JSON/text response that I can print or process in Python.

    What actually happened: Instead, I got the error:

    Ollama error: write() argument must be str, not bytes

    Continue reading...

Compartilhe esta Página