Skip to content

Commit edf44eb

Browse files
test(api): pin how POST /scores responses parse into the union (#1964)
* test(api): pin how POST /scores responses parse into the union Cover the 200, 202 and 207 response shapes of scores.create, sync and async, against a mocked transport. CI also runs them on pydantic 2.7, whose union matching depends on the member order. Co-authored-by: Hassieb Pakzad <hassiebp@users.noreply.github.com> * test(api): assert the pydantic 2.7 overlay and describe its coverage Co-authored-by: Hassieb Pakzad <hassiebp@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Hassieb Pakzad <hassiebp@users.noreply.github.com>
1 parent e99cc02 commit edf44eb

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ jobs:
9898
python --version
9999
uv run --frozen pytest -n auto --dist worksteal -s -v --log-cli-level=INFO tests/unit
100100
101+
# pydantic < 2.8 matches undiscriminated unions left to right, so the
102+
# CreateScoresResponse member order matters there.
103+
- name: Run score response parsing on pydantic 2.7
104+
if: ${{ matrix.python-version == '3.11' }}
105+
run: |
106+
uv run --frozen --with "pydantic==2.7.4" python -c "import pydantic; assert pydantic.VERSION == '2.7.4', pydantic.VERSION"
107+
uv run --frozen --with "pydantic==2.7.4" pytest -v tests/unit/test_scores_batch_response.py
108+
101109
e2e-tests:
102110
runs-on: ubuntu-latest
103111
timeout-minutes: 30
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""Parsing of POST /api/public/scores responses into the CreateScoresResponse union.
2+
3+
The union is undiscriminated, so the model is picked from the response body. CI
4+
also runs this file on pydantic 2.7, an older supported minor whose union matching
5+
differs from current releases.
6+
"""
7+
8+
import json
9+
from typing import Any, List
10+
11+
import httpx
12+
import pytest
13+
14+
from langfuse.api import AsyncLangfuseAPI, LangfuseAPI
15+
from langfuse.api.scores.types.create_score_batch_response import (
16+
CreateScoreBatchResponse,
17+
)
18+
from langfuse.api.scores.types.create_score_batch_results import (
19+
CreateScoreBatchResults,
20+
)
21+
from langfuse.api.scores.types.create_score_request import CreateScoreRequest
22+
from langfuse.api.scores.types.create_score_response import CreateScoreResponse
23+
24+
SINGLE_REQUEST = CreateScoreRequest(name="accuracy", value=0.9, trace_id="trace-1")
25+
BATCH_REQUEST = [
26+
CreateScoreRequest(name="accuracy", value=0.9, trace_id="trace-1"),
27+
CreateScoreRequest(name="tone", value="friendly", trace_id="trace-1"),
28+
]
29+
30+
CASES = [
31+
pytest.param(SINGLE_REQUEST, 200, {"id": "score-1"}, id="single-200"),
32+
pytest.param(BATCH_REQUEST, 202, {"message": "Accepted"}, id="batch-202"),
33+
pytest.param(
34+
BATCH_REQUEST,
35+
207,
36+
{
37+
"accepted": 1,
38+
"rejected": 1,
39+
"errors": [{"message": "tone: value must be numeric"}],
40+
},
41+
id="batch-207",
42+
),
43+
]
44+
45+
46+
def _handler(status: int, body: dict, requests: List[httpx.Request]) -> Any:
47+
def handle(request: httpx.Request) -> httpx.Response:
48+
requests.append(request)
49+
return httpx.Response(status, json=body)
50+
51+
return handle
52+
53+
54+
def _assert_parsed(response: Any, status: int, body: dict) -> None:
55+
if status == 200:
56+
assert isinstance(response, CreateScoreResponse)
57+
assert response.id == body["id"]
58+
elif status == 202:
59+
assert isinstance(response, CreateScoreBatchResponse)
60+
assert response.message == "Accepted"
61+
else:
62+
assert isinstance(response, CreateScoreBatchResults)
63+
assert (response.accepted, response.rejected) == (1, 1)
64+
assert [error.message for error in response.errors] == [
65+
"tone: value must be numeric"
66+
]
67+
68+
69+
def _assert_sent(requests: List[httpx.Request], request: Any) -> None:
70+
assert len(requests) == 1
71+
assert requests[0].url.path == "/api/public/scores"
72+
sent = json.loads(requests[0].content)
73+
assert isinstance(sent, list) == isinstance(request, list)
74+
75+
76+
@pytest.mark.parametrize(("request_body", "status", "body"), CASES)
77+
def test_sync_create_parses_each_response_shape(request_body, status, body):
78+
requests: List[httpx.Request] = []
79+
client = LangfuseAPI(
80+
base_url="http://langfuse.test",
81+
username="pk",
82+
password="sk",
83+
httpx_client=httpx.Client(
84+
transport=httpx.MockTransport(_handler(status, body, requests))
85+
),
86+
)
87+
88+
response = client.scores.create(request=request_body)
89+
90+
_assert_parsed(response, status, body)
91+
_assert_sent(requests, request_body)
92+
93+
94+
@pytest.mark.asyncio
95+
@pytest.mark.parametrize(("request_body", "status", "body"), CASES)
96+
async def test_async_create_parses_each_response_shape(request_body, status, body):
97+
requests: List[httpx.Request] = []
98+
client = AsyncLangfuseAPI(
99+
base_url="http://langfuse.test",
100+
username="pk",
101+
password="sk",
102+
httpx_client=httpx.AsyncClient(
103+
transport=httpx.MockTransport(_handler(status, body, requests))
104+
),
105+
)
106+
107+
response = await client.scores.create(request=request_body)
108+
109+
_assert_parsed(response, status, body)
110+
_assert_sent(requests, request_body)

0 commit comments

Comments
 (0)