mirror of
https://github.com/ollama/ollama-python.git
synced 2026-01-13 21:57:16 +08:00
parse json error
This commit is contained in:
parent
7c2ec01d2f
commit
64127ee821
15
README.md
15
README.md
@ -68,3 +68,18 @@ async def 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
|
||||
|
||||
import sys
|
||||
@ -145,6 +146,13 @@ class ResponseError(Exception):
|
||||
"""
|
||||
|
||||
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)
|
||||
self.content = content
|
||||
"Reason for the error."
|
||||
|
||||
Loading…
Reference in New Issue
Block a user