Skip to content

Commit b0da4ff

Browse files
committed
no head
this request is unnecessary since the POST will short circuit the request if the blob already exists
1 parent 0bbc246 commit b0da4ff

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

ollama/_client.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -523,14 +523,8 @@ def _create_blob(self, path: Union[str, Path]) -> str:
523523

524524
digest = f'sha256:{sha256sum.hexdigest()}'
525525

526-
try:
527-
self._request_raw('HEAD', f'/api/blobs/{digest}')
528-
except ResponseError as e:
529-
if e.status_code != 404:
530-
raise
531-
532-
with open(path, 'rb') as r:
533-
self._request_raw('POST', f'/api/blobs/{digest}', content=r)
526+
with open(path, 'rb') as r:
527+
self._request_raw('POST', f'/api/blobs/sha256:{digest}', content=r)
534528

535529
return digest
536530

@@ -1007,21 +1001,15 @@ async def _create_blob(self, path: Union[str, Path]) -> str:
10071001

10081002
digest = f'sha256:{sha256sum.hexdigest()}'
10091003

1010-
try:
1011-
await self._request_raw('HEAD', f'/api/blobs/{digest}')
1012-
except ResponseError as e:
1013-
if e.status_code != 404:
1014-
raise
1015-
1016-
async def upload_bytes():
1017-
with open(path, 'rb') as r:
1018-
while True:
1019-
chunk = r.read(32 * 1024)
1020-
if not chunk:
1021-
break
1022-
yield chunk
1023-
1024-
await self._request_raw('POST', f'/api/blobs/{digest}', content=upload_bytes())
1004+
async def upload_bytes():
1005+
with open(path, 'rb') as r:
1006+
while True:
1007+
chunk = r.read(32 * 1024)
1008+
if not chunk:
1009+
break
1010+
yield chunk
1011+
1012+
await self._request_raw('POST', f'/api/blobs/{digest}', content=upload_bytes())
10251013

10261014
return digest
10271015

tests/test_client.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def generate():
293293

294294

295295
def test_client_create_path(httpserver: HTTPServer):
296-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
296+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
297297
httpserver.expect_ordered_request(
298298
'/api/create',
299299
method='POST',
@@ -316,7 +316,7 @@ def test_client_create_path(httpserver: HTTPServer):
316316

317317

318318
def test_client_create_path_relative(httpserver: HTTPServer):
319-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
319+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
320320
httpserver.expect_ordered_request(
321321
'/api/create',
322322
method='POST',
@@ -348,7 +348,7 @@ def userhomedir():
348348

349349

350350
def test_client_create_path_user_home(httpserver: HTTPServer, userhomedir):
351-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
351+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
352352
httpserver.expect_ordered_request(
353353
'/api/create',
354354
method='POST',
@@ -371,7 +371,7 @@ def test_client_create_path_user_home(httpserver: HTTPServer, userhomedir):
371371

372372

373373
def test_client_create_modelfile(httpserver: HTTPServer):
374-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
374+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
375375
httpserver.expect_ordered_request(
376376
'/api/create',
377377
method='POST',
@@ -390,7 +390,7 @@ def test_client_create_modelfile(httpserver: HTTPServer):
390390

391391

392392
def test_client_create_modelfile_roundtrip(httpserver: HTTPServer):
393-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
393+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
394394
httpserver.expect_ordered_request(
395395
'/api/create',
396396
method='POST',
@@ -455,7 +455,6 @@ def test_client_create_from_library(httpserver: HTTPServer):
455455

456456

457457
def test_client_create_blob(httpserver: HTTPServer):
458-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=404))
459458
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=201))
460459

461460
client = Client(httpserver.url_for('/'))
@@ -466,7 +465,7 @@ def test_client_create_blob(httpserver: HTTPServer):
466465

467466

468467
def test_client_create_blob_exists(httpserver: HTTPServer):
469-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
468+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
470469

471470
client = Client(httpserver.url_for('/'))
472471

@@ -774,7 +773,7 @@ def generate():
774773

775774
@pytest.mark.asyncio
776775
async def test_async_client_create_path(httpserver: HTTPServer):
777-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
776+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
778777
httpserver.expect_ordered_request(
779778
'/api/create',
780779
method='POST',
@@ -798,7 +797,7 @@ async def test_async_client_create_path(httpserver: HTTPServer):
798797

799798
@pytest.mark.asyncio
800799
async def test_async_client_create_path_relative(httpserver: HTTPServer):
801-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
800+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
802801
httpserver.expect_ordered_request(
803802
'/api/create',
804803
method='POST',
@@ -822,7 +821,7 @@ async def test_async_client_create_path_relative(httpserver: HTTPServer):
822821

823822
@pytest.mark.asyncio
824823
async def test_async_client_create_path_user_home(httpserver: HTTPServer, userhomedir):
825-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
824+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
826825
httpserver.expect_ordered_request(
827826
'/api/create',
828827
method='POST',
@@ -846,7 +845,7 @@ async def test_async_client_create_path_user_home(httpserver: HTTPServer, userho
846845

847846
@pytest.mark.asyncio
848847
async def test_async_client_create_modelfile(httpserver: HTTPServer):
849-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
848+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
850849
httpserver.expect_ordered_request(
851850
'/api/create',
852851
method='POST',
@@ -866,7 +865,7 @@ async def test_async_client_create_modelfile(httpserver: HTTPServer):
866865

867866
@pytest.mark.asyncio
868867
async def test_async_client_create_modelfile_roundtrip(httpserver: HTTPServer):
869-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
868+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
870869
httpserver.expect_ordered_request(
871870
'/api/create',
872871
method='POST',
@@ -933,7 +932,6 @@ async def test_async_client_create_from_library(httpserver: HTTPServer):
933932

934933
@pytest.mark.asyncio
935934
async def test_async_client_create_blob(httpserver: HTTPServer):
936-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=404))
937935
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=201))
938936

939937
client = AsyncClient(httpserver.url_for('/'))
@@ -945,7 +943,7 @@ async def test_async_client_create_blob(httpserver: HTTPServer):
945943

946944
@pytest.mark.asyncio
947945
async def test_async_client_create_blob_exists(httpserver: HTTPServer):
948-
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
946+
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
949947

950948
client = AsyncClient(httpserver.url_for('/'))
951949

0 commit comments

Comments
 (0)