mirror of
https://github.com/kharonsec/ollama-python
synced 2026-04-25 23:24:55 +02:00
parse json error
This commit is contained in:
15
README.md
15
README.md
@@ -68,3 +68,18 @@ async def chat():
|
|||||||
|
|
||||||
asyncio.run(chat())
|
asyncio.run(chat())
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Handling Errors
|
||||||
|
|
||||||
|
Errors are raised if requests return an error status or if an error is detected while streaming.
|
||||||
|
|
||||||
|
```python
|
||||||
|
model = 'does-not-yet-exist'
|
||||||
|
|
||||||
|
try:
|
||||||
|
ollama.chat(model)
|
||||||
|
except ollama.ResponseError as e:
|
||||||
|
print('Error:', e.content)
|
||||||
|
if e.status_code == 404:
|
||||||
|
ollama.pull(model)
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
from typing import Any, TypedDict, Sequence, Literal
|
from typing import Any, TypedDict, Sequence, Literal
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
@@ -145,6 +146,13 @@ class ResponseError(Exception):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, content: str, status_code: int = -1):
|
def __init__(self, content: str, status_code: int = -1):
|
||||||
|
try:
|
||||||
|
# try to parse content as JSON and extract 'error'
|
||||||
|
# fallback to raw content if JSON parsing fails
|
||||||
|
content = json.loads(content).get('error', content)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
...
|
||||||
|
|
||||||
super().__init__(content)
|
super().__init__(content)
|
||||||
self.content = content
|
self.content = content
|
||||||
"Reason for the error."
|
"Reason for the error."
|
||||||
|
|||||||
Reference in New Issue
Block a user