Skip to content

Commit dc38fe4

Browse files
authored
Merge pull request #277 from ollama/mxyng/no-head
no head request for create blob
2 parents a92f111 + b0da4ff commit dc38fe4

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
@@ -528,14 +528,8 @@ def _create_blob(self, path: Union[str, Path]) -> str:
528528

529529
digest = f'sha256:{sha256sum.hexdigest()}'
530530

531-
try:
532-
self._request_raw('HEAD', f'/api/blobs/{digest}')
533-
except ResponseError as e:
534-
if e.status_code != 404:
535-
raise
536-
537-
with open(path, 'rb') as r:
538-
self._request_raw('POST', f'/api/blobs/{digest}', content=r)
531+
with open(path, 'rb') as r:
532+
self._request_raw('POST', f'/api/blobs/sha256:{digest}', content=r)
539533

540534
return digest
541535

@@ -1012,21 +1006,15 @@ async def _create_blob(self, path: Union[str, Path]) -> str:
10121006

10131007
digest = f'sha256:{sha256sum.hexdigest()}'
10141008

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

10311019
return digest
10321020

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)