Troubleshooting of Ollama API: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
(Created page with " 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 "json: cannot unmarshal object ..." == '''Error message:''' <pre> HTTP Error 400: {"error": "json: cannot unmarshal object into Go struct field ChatRequest.messages of type []api.Mess...")
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 10: Line 10:
HTTP Error 400: {"error": "json: cannot unmarshal object into Go struct field ChatRequest.messages of type []api.Message"}
HTTP Error 400: {"error": "json: cannot unmarshal object into Go struct field ChatRequest.messages of type []api.Message"}
</pre>
</pre>
'''Applicable Model:'''
* {{kbd | key=gpt-oss:120b}}


'''Solution'''
'''Solution'''
Line 15: Line 19:
Root Cause: Incorrect `messages` format. The API expects an array format, but a single object was being sent.
Root Cause: Incorrect `messages` format. The API expects an array format, but a single object was being sent.


Incorrect Format: ❌ This is an object
Incorrect Format in PHP: ❌ This is an object
<pre>
<pre>
'messages' => ['role' => 'user', 'content' => $message]
'messages' => ['role' => 'user', 'content' => $message]
</pre>
</pre>


Correct Format: ✅ This is an array containing an object
Correct Format in PHP: ✅ This is an array containing an object
<pre>
<pre>
'messages' => [['role' => 'user', 'content' => $message]]   
'messages' => [['role' => 'user', 'content' => $message]]   
Line 29: Line 33:


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]]  

Latest revision as of 10:46, 3 September 2025

Troubleshooting of Ollama API

Raise_hand.png If you have any questions about the Ollama API, you can post them on the Ollama GitHub Repository.

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]