Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions portkey_ai/api_resources/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,7 @@ async def _build_request(self, options: Options) -> httpx.Request:
headers=headers,
params=params,
json=json_body,
files=options.files,
timeout=options.timeout,
)
return request
Expand Down
58 changes: 58 additions & 0 deletions tests/test_post_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from __future__ import annotations

from typing import Dict

import httpx
import pytest

from portkey_ai import AsyncPortkey, Portkey

BASE_URL = "https://api.portkey.ai/v1"


def _capture_transport(captured: Dict[str, object]) -> httpx.MockTransport:
def handler(request: httpx.Request) -> httpx.Response:
captured["content_type"] = request.headers.get("content-type", "")
captured["content"] = request.content
return httpx.Response(200, json={"success": True})

return httpx.MockTransport(handler)


def test_sync_post_sends_multipart_files() -> None:
captured: Dict[str, object] = {}
http_client = httpx.Client(
transport=_capture_transport(captured), base_url=BASE_URL
)
client = Portkey(api_key="dummy", base_url=BASE_URL, http_client=http_client)

client.post(
url="/files",
purpose="assistants",
files={"file": ("hello.txt", b"hello world", "text/plain")},
)

assert "multipart/form-data" in captured["content_type"]
body = captured["content"]
assert b"hello.txt" in body
assert b"hello world" in body
Comment on lines +22 to +38


@pytest.mark.asyncio
async def test_async_post_sends_multipart_files() -> None:
captured: Dict[str, object] = {}
http_client = httpx.AsyncClient(
transport=_capture_transport(captured), base_url=BASE_URL
)
client = AsyncPortkey(api_key="dummy", base_url=BASE_URL, http_client=http_client)

await client.post(
url="/files",
purpose="assistants",
files={"file": ("hello.txt", b"hello world", "text/plain")},
)

assert "multipart/form-data" in captured["content_type"]
body = captured["content"]
assert b"hello.txt" in body
assert b"hello world" in body
Comment on lines +41 to +58