Troubleshooting of Ollama API: Difference between revisions

Tag: wikieditor
Tag: wikieditor
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:


{{Raise hand | text = If you have any questions about the Ollama API, you can post them on the [https://github.com/ollama/ollama/issues Ollama GitHub Repository].}}
{{Raise hand | text = If you have any questions about the Ollama API, you can post them on the [https://github.com/ollama/ollama/issues Ollama GitHub Repository].}}
== How to Fix "Unauthorized" Error ==
'''Error message:'''
<pre>
HTTP Error 401
Response: {"error":"Unauthorized"}
</pre>
'''Solution'''
Verify that your API key is correct by testing it with the following curl command:
<pre>
curl -s -o /dev/null -w "%{http_code}\n" -X POST https://ollama.com/api/chat -H "Authorization: Bearer REPLACE_WITH_YOUR_API_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-oss:120b","messages":[{"role":"user","content":"hi"}],"stream":false}'
</pre>
Check the returned status code:
* '''200''' — the API key is valid
* '''401''' — the API key is invalid or incorrect
== How to fix "prompt too long; exceeded max context length" when sending files ==
'''Error message:'''
<pre>
HTTP Error 400: {"error":"prompt too long; exceeded max context length by 311181 tokens"}
</pre>
'''Applicable Model:'''
* {{kbd | key=gpt-oss:120b}}
'''Solution'''
Root Cause: The entire file content was concatenated into <code>content</code> as raw text. Ollama's <code>/api/chat</code> has no dedicated "file" field — text/document content must be extracted client-side and embedded directly into the message string, which counts fully against <code>num_ctx</code>.
Incorrect Usage: Whole file dumped straight into content
<pre>
'messages' => [[
    'role' => 'user',
    'content' => file''get''contents($path) . "\n\nPlease summary this document"
]]
</pre>
Correct Usage: Truncate / chunk / summarize before sending, and raise <code>num_ctx</code> to match
<pre>
'messages' => [[
    'role' => 'user',
    'content' => $chunkedText . "\n\nPlease summary this document"
]],
'options' => [
    'num_ctx' => 32768
]
</pre>
'''Explanation:'''
Ollama's chat endpoint only has a native <code>images</code> array (base64-encoded, vision models only). There is no <code>files</code> or <code>document</code> parameter. Any non-image file — PDF, txt, docx, log — must be converted to plain text on the client and inserted into <code>content</code>, so its size is not free; it consumes tokens exactly like typed text.
If the resulting text exceeds the model's context window (<code>num_ctx</code>, default is often small, e.g. 2048–4096), the request fails with <code>"prompt too long"</code>. Fix by combining:
* Explicitly setting a larger <code>num_ctx</code> in <code>options</code> (bounded by what the model/hardware can actually support)
* Chunking the file and sending it in multiple requests
* Pre-summarizing or using RAG-style retrieval so only relevant excerpts are sent, instead of the full file
This fix resolves the <code>"prompt too long; exceeded max context length"</code> error when large files are pasted into chat.


== How to fix "json: cannot unmarshal object ..." ==
== How to fix "json: cannot unmarshal object ..." ==
Line 33: Line 98:


This fix resolves the `"cannot unmarshal object into Go struct field ChatRequest.messages"` error.
This fix resolves the `"cannot unmarshal object into Go struct field ChatRequest.messages"` error.
== Related links ==
* Forum: [https://www.reddit.com/r/ollama/ ollama]


[[Category: Tools]]  
[[Category: Tools]]