"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from typing import Any, Optional, Type, TypeVar, overload

import httpx

from .serializers import unmarshal_json
from openrouter import errors

T = TypeVar("T")


@overload
def unmarshal_json_response(
    typ: Type[T], http_res: httpx.Response, body: Optional[str] = None
) -> T: ...


@overload
def unmarshal_json_response(
    typ: Any, http_res: httpx.Response, body: Optional[str] = None
) -> Any: ...


def unmarshal_json_response(
    typ: Any, http_res: httpx.Response, body: Optional[str] = None
) -> Any:
    if body is None:
        body = http_res.text
    try:
        return unmarshal_json(body, typ)
    except Exception as e:
        raise errors.ResponseValidationError(
            "Response validation failed",
            http_res,
            e,
            body,
        ) from e
