Troubleshooting of Ollama API: Difference between revisions
(How to Fix "Unauthorized" Error) |
|||
| Line 23: | Line 23: | ||
* '''200''' — the API key is valid | * '''200''' — the API key is valid | ||
* '''401''' — the API key is invalid or incorrect | * '''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 ..." == | ||
Latest revision as of 17:03, 12 August 2026
If you have any questions about the Ollama API, you can post them on the Ollama GitHub Repository.
How to Fix "Unauthorized" Error[edit]
Error message:
HTTP Error 401
Response: {"error":"Unauthorized"}
Solution
Verify that your API key is correct by testing it with the following curl command:
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}'
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[edit]
Error message:
HTTP Error 400: {"error":"prompt too long; exceeded max context length by 311181 tokens"}
Applicable Model:
- gpt-oss:120b
Solution
Root Cause: The entire file content was concatenated into content as raw text. Ollama's /api/chat has no dedicated "file" field — text/document content must be extracted client-side and embedded directly into the message string, which counts fully against num_ctx.
Incorrect Usage: Whole file dumped straight into content
'messages' => [[
'role' => 'user',
'content' => file''get''contents($path) . "\n\nPlease summary this document"
]]
Correct Usage: Truncate / chunk / summarize before sending, and raise num_ctx to match
'messages' => [[
'role' => 'user',
'content' => $chunkedText . "\n\nPlease summary this document"
]],
'options' => [
'num_ctx' => 32768
]
Explanation:
Ollama's chat endpoint only has a native images array (base64-encoded, vision models only). There is no files or document parameter. Any non-image file — PDF, txt, docx, log — must be converted to plain text on the client and inserted into content, so its size is not free; it consumes tokens exactly like typed text.
If the resulting text exceeds the model's context window (num_ctx, default is often small, e.g. 2048–4096), the request fails with "prompt too long". Fix by combining:
- Explicitly setting a larger
num_ctxinoptions(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 "prompt too long; exceeded max context length" error when large files are pasted into chat.
How to fix "json: cannot unmarshal object ..."[edit]
Error message:
HTTP Error 400: {"error": "json: cannot unmarshal object into Go struct field ChatRequest.messages of type []api.Message"}
Applicable Model:
- gpt-oss:120b
Solution
Root Cause: Incorrect `messages` format. The API expects an array format, but a single object was being sent.
Incorrect Format in PHP: ❌ This is an object
'messages' => ['role' => 'user', 'content' => $message]
Correct Format in PHP: ✅ This is an array containing an object
'messages' => [['role' => 'user', 'content' => $message]]
Explanation: The `messages` parameter must be an array of message objects, not a single message object. Each message object should contain `role` and `content` properties. The extra square brackets `[]` wrap the message object into an array, which matches the API's expected format.
This fix resolves the `"cannot unmarshal object into Go struct field ChatRequest.messages"` error.
Related links[edit]
- Forum: ollama