[PR #199] [CLOSED] fix: Ensure JSON response is properly parsed in generate function when format is set to 'json' #407

Closed
opened 2026-02-15 16:30:17 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama-python/pull/199
Author: @wyy511511
Created: 6/28/2024
Status: Closed

Base: mainHead: main


📝 Commits (1)

  • eb0077c fix: Ensure JSON response is properly parsed in generate function when format is set to 'json'

📊 Changes

1 file changed (+8 additions, -1 deletions)

View changed files

📝 ollama/_client.py (+8 -1)

📄 Description

When I set format as "json", my response looks like this:

{'model': 'llama3', 'created_at': '2024-06-28T05:44:32.12715Z', 'response': '{"results": [\n{\n"entity_type": "IMEI",\n"text": "06-184755-866851-3"\n}\n]}\n\n \n\n ', 'done': True, 'done_reason': 'stop', 'context': [], 'total_duration': 4528553708, 'load_duration': 2316500, 'prompt_eval_count': 437, 'prompt_eval_duration': 2743245000, 'eval_count': 32, 'eval_duration': 1781380000}

As you can notice, although it indeed returns a JSON object, the previous code parses the 'response' as a string (hence the presence of \n characters and type checking result).

After I added some code to correctly parse the JSON response, the response now looks like this:

{'model': 'llama3', 'created_at': '2024-06-28T09:16:56.417739Z', 'response': {'results': [{'entity_type': 'IMEI', 'text': '06-184755-866851-3'}]}, 'done': True, 'done_reason': 'stop', 'context':

It becomes cleaner and matches my expected format.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama-python/pull/199 **Author:** [@wyy511511](https://github.com/wyy511511) **Created:** 6/28/2024 **Status:** ❌ Closed **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (1) - [`eb0077c`](https://github.com/ollama/ollama-python/commit/eb0077c1cb2b9c970a0d2422cc419d21a6d7b01f) fix: Ensure JSON response is properly parsed in generate function when format is set to 'json' ### 📊 Changes **1 file changed** (+8 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `ollama/_client.py` (+8 -1) </details> ### 📄 Description When I set` format` as `"json"`, my response looks like this: `{'model': 'llama3', 'created_at': '2024-06-28T05:44:32.12715Z', 'response': '{"results": [\n{\n"entity_type": "IMEI",\n"text": "06-184755-866851-3"\n}\n]}\n\n \n\n ', 'done': True, 'done_reason': 'stop', 'context': [], 'total_duration': 4528553708, 'load_duration': 2316500, 'prompt_eval_count': 437, 'prompt_eval_duration': 2743245000, 'eval_count': 32, 'eval_duration': 1781380000}` As you can notice, although it indeed returns a JSON object, the previous code parses the `'response'` as a string (hence the presence of `\n` characters and type checking result). After I added some code to correctly parse the JSON response, the response now looks like this: `{'model': 'llama3', 'created_at': '2024-06-28T09:16:56.417739Z', 'response': {'results': [{'entity_type': 'IMEI', 'text': '06-184755-866851-3'}]}, 'done': True, 'done_reason': 'stop', 'context':` It becomes cleaner and matches my expected format. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-15 16:30:17 -05:00
yindo closed this issue 2026-02-15 16:30:17 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#407