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
18 changes: 14 additions & 4 deletions promptlens/providers/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any, Dict, List, Optional

import aiohttp
from aiohttp import ContentTypeError

from promptlens.models.config import ProviderConfig
from promptlens.models.result import ModelResponse
Expand Down Expand Up @@ -59,6 +60,11 @@ def _extract_content(data: Dict[str, Any]) -> str:

return ""

@staticmethod
def _extract_content_from_text_response(text: str) -> str:
"""Extract usable content from plain-text HTTP responses."""
return text.strip()

def __init__(self, config: ProviderConfig) -> None:
"""Initialize the HTTP provider.

Expand Down Expand Up @@ -119,10 +125,14 @@ async def _make_request() -> ModelResponse:
timeout=aiohttp.ClientTimeout(total=self.config.timeout),
) as response:
response.raise_for_status()
data = await response.json()

# Extract content (try common response formats)
content = self._extract_content(data)
try:
data = await response.json()
content = self._extract_content(data)
except ContentTypeError:
text = await response.text()
content = self._extract_content_from_text_response(text)

# Extracted content from JSON shape or plain text body.

# Local models typically don't provide token counts or cost
return ModelResponse(
Expand Down
6 changes: 6 additions & 0 deletions tests/test_http_provider_response_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ def test_extract_content_returns_empty_for_unknown_shape() -> None:
provider = _provider()

assert provider._extract_content({"foo": "bar"}) == ""


def test_extract_content_from_text_response_strips_whitespace() -> None:
provider = _provider()

assert provider._extract_content_from_text_response(" hello world\n") == "hello world"