Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

5.5 KB
Raw Blame
from collections.abc import Mapping
from types import MappingProxyType
from typing import Final

import pytest
from pydantic import TypeAdapter

import litellm
from litellm.proxy.roi_calculator.estimator import Estimator, estimator_options
from litellm.proxy.roi_calculator.github import SourceError
from litellm.types.roi_calculator import (
    ROICompletionRequest,
    ROIEstimatorChanges,
    ROIEstimatorEvidence,
    ROIPullEvidence,
    ROIResponseFormat,
    ROISettings,
)
from litellm.utils import supports_none_reasoning_effort


def _pull() -> ROIPullEvidence:
    pull: Final[ROIPullEvidence] = {
        "repo": "org/repo",
        "number": 42,
        "title": "Fix timezone conversion",
        "body": "Preserve UTC behavior.",
        "url": "https://github.com/org/repo/pull/42",
        "login": "alice",
        "emails": ("[email protected]",),
        "profile_email": "[email protected]",
        "merged_at": "2026-09-12T12:00:00Z",
        "head_sha": "abcdef",
        "additions": 1,
        "deletions": 1,
        "changed_files": 1,
        "files": ({"filename": "time.py", "status": "modified", "additions": 1, "deletions": 1},),
        "commits": ({"sha": "abcdef", "message": "Fix timezone conversion"},),
        "commit_count": 1,
        "incomplete_metadata": False,
    }
    return pull


def _settings() -> ROISettings:
    return ROISettings(estimator_model="test-estimator")


def _model_with_none_reasoning_effort() -> str:
    return next(
        model
        for model, metadata in litellm.model_cost.items()
        if metadata.get("supports_none_reasoning_effort") is True and supports_none_reasoning_effort(model)
    )


def _completion(content: str) -> Mapping[str, object]:
    message: Final = MappingProxyType({"content": content})
    choice: Final = MappingProxyType({"finish_reason": "stop", "message": message})
    response: Final = MappingProxyType({"choices": (choice,)})
    return response


@pytest.mark.parametrize(
    "content",
    (
        '{"hours": 4.25, "reasoning": "Timezone conversion and regression verification."}',
        '```json\n{"hours": 4.25, "reasoning": "Timezone conversion and regression verification."}\n```',
        'The estimate is:\n{"hours": 4.25, "reasoning": "Timezone conversion and regression verification."}\nDone.',
    ),
)
@pytest.mark.asyncio
async def test_estimator_sends_metadata_only_json_request_and_parses_valid_result(content: str) -> None:
    async def complete(request: ROICompletionRequest) -> object:
        assert request.reasoning_effort is None
        evidence: Final = TypeAdapter(ROIEstimatorEvidence).validate_json(request.messages[1]["content"])
        assert request.temperature == 0
        expected_response_format: Final[ROIResponseFormat] = {"type": "json_object"}
        assert request.response_format == expected_response_format
        assert "patch" not in request.messages[1]["content"]
        assert "[email protected]" not in request.messages[1]["content"]
        expected_changes: Final = ROIEstimatorChanges(additions=1, deletions=1, files=1, commits=1)
        assert evidence.changes == expected_changes
        assert evidence.commits[0].message == "Fix timezone conversion"
        assert "without AI assistance" in request.messages[0]["content"]
        return _completion(content)

    result: Final = await Estimator(_settings(), complete).estimate(_pull())

    assert result["hours"] == 4.25
    assert result.get("effort_basis") == "without_ai"


def test_estimator_options_follow_underlying_model_metadata() -> None:
    supported_model: Final = _model_with_none_reasoning_effort()

    assert estimator_options(((supported_model, None),)) == {"reasoning_effort": "none"}
    assert estimator_options(((supported_model, None), ("unknown-model", None))) == {}
    assert estimator_options((("unknown-model", None),)) == {}


@pytest.mark.asyncio
async def test_estimator_sets_none_reasoning_effort_for_supported_underlying_model() -> None:
    supported_model: Final = _model_with_none_reasoning_effort()

    async def complete(request: ROICompletionRequest) -> object:
        assert request.reasoning_effort == "none"
        return _completion('{"hours": 1, "reasoning": "Metadata-backed capability."}')

    result: Final = await Estimator(_settings(), complete, ((supported_model, None),)).estimate(_pull())

    assert result["hours"] == 1


@pytest.mark.parametrize(
    "content",
    (
        '{"hours": -1, "reasoning": "invalid"}',
        '{"hours": NaN, "reasoning": "invalid"}',
        '{"hours": "4", "reasoning": "invalid"}',
        '{"hours": true, "reasoning": "invalid"}',
        '{"hours": 4}',
        '{"hours": 4, "reasoning": "  "}',
        '```json\n{"hours": -1, "reasoning": "invalid"}\n```',
        '```json\n{"hours": "4", "reasoning": "invalid"}\n```',
        "not json",
    ),
)
@pytest.mark.asyncio
async def test_estimator_rejects_invalid_hours_or_reasoning(content: str) -> None:
    async def complete(request: ROICompletionRequest) -> object:
        return _completion(content)

    with pytest.raises(SourceError):
        await Estimator(_settings(), complete).estimate(_pull())


@pytest.mark.asyncio
async def test_incomplete_metadata_is_not_sent_to_the_estimator() -> None:
    async def complete(request: ROICompletionRequest) -> object:
        raise AssertionError("Incomplete metadata must not reach the estimator.")

    pull: Final[ROIPullEvidence] = {**_pull(), "incomplete_metadata": True}

    result: Final = await Estimator(_settings(), complete).estimate(pull)

    assert result["status"] == "needs_review"