Skip to content

Commit 7205218

Browse files
ParthSareenmxyng
authored andcommitted
Fixing empty header + ensuring security (#313)
* Fixing empty header + ensuring security
1 parent f258342 commit 7205218

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

ollama/_client.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,16 @@ def __init__(
9090
base_url=_parse_host(host or os.getenv('OLLAMA_HOST')),
9191
follow_redirects=follow_redirects,
9292
timeout=timeout,
93+
# Lowercase all headers to ensure override
9394
headers={
94-
'Content-Type': 'application/json',
95-
'Accept': 'application/json',
96-
'User-Agent': f'ollama-python/{__version__} ({platform.machine()} {platform.system().lower()}) Python/{platform.python_version()}',
97-
}.update(headers or {}),
95+
k.lower(): v
96+
for k, v in {
97+
**(headers or {}),
98+
'Content-Type': 'application/json',
99+
'Accept': 'application/json',
100+
'User-Agent': f'ollama-python/{__version__} ({platform.machine()} {platform.system().lower()}) Python/{platform.python_version()}',
101+
}.items()
102+
},
98103
**kwargs,
99104
)
100105

tests/test_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,3 +968,19 @@ async def test_async_client_copy(httpserver: HTTPServer):
968968
client = AsyncClient(httpserver.url_for('/api/copy'))
969969
response = await client.copy('dum', 'dummer')
970970
assert response['status'] == 'success'
971+
972+
973+
def test_headers():
974+
client = Client()
975+
assert client._client.headers['content-type'] == 'application/json'
976+
assert client._client.headers['accept'] == 'application/json'
977+
assert client._client.headers['user-agent'].startswith('ollama-python/')
978+
979+
client = Client(
980+
headers={
981+
'X-Custom': 'value',
982+
'Content-Type': 'text/plain',
983+
}
984+
)
985+
assert client._client.headers['x-custom'] == 'value'
986+
assert client._client.headers['content-type'] == 'application/json'

0 commit comments

Comments
 (0)