|
| 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