Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
LemonWiki共筆
Search
Search
Appearance
Log in
Personal tools
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Troubleshooting of Ollama API
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
Troubleshooting of [https://ollama.com/ Ollama] [https://github.com/ollama/ollama/blob/main/docs/api.md API] {{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 ..." == '''Error message:''' <pre> HTTP Error 400: {"error": "json: cannot unmarshal object into Go struct field ChatRequest.messages of type []api.Message"} </pre> '''Applicable Model:''' * {{kbd | key=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 <pre> 'messages' => ['role' => 'user', 'content' => $message] </pre> Correct Format in PHP: ✅ This is an array containing an object <pre> 'messages' => [['role' => 'user', 'content' => $message]] </pre> '''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 == * Forum: [https://www.reddit.com/r/ollama/ ollama] [[Category: Tools]] [[Category: Ollama]] [[Category: Revised with LLMs]] [[Category: Generative AI]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki共筆:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Kbd
(
edit
)
Template:Raise hand
(
edit
)