Skip to content

Base

MissingRawChatResponseError

Bases: Exception

Raised when a ChatResponse is missing the raw attribute.

RawMessageExtractionError

Bases: Exception

Raised when a message cannot be extracted from a ChatResponse raw attribute.

MissingResponseContentError

Bases: Exception

Raised when the LLM response message has no content.

EmptyResponseMessageError

Bases: Exception

Raised when the LLM response message is empty.

ReasoningExtractionError

Bases: Exception

Raised when reasoning cannot be extracted from the LLM response message.

EmptyReasoningError

Bases: Exception

Raised when the extracted reasoning is empty.

LLM

Bases: ABC

call_llm_chat_with_guided_decoding(messages, *, json_schema=None, **request_kwargs) abstractmethod

Call a chat LLM with optional json schema for guided decoding.

Source code in src/kibad_llm/llms/base.py
52
53
54
55
56
57
58
59
60
61
@abstractmethod
def call_llm_chat_with_guided_decoding(
    self,
    messages: list[SimpleChatMessage],
    *,
    json_schema: dict[str, Any] | None = None,
    **request_kwargs,
) -> ChatResponse:
    """Call a chat LLM with optional json schema for guided decoding."""
    ...

get_raw_message_from_chat_response(response)

Extract raw message from a chat response.

Source code in src/kibad_llm/llms/base.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def get_raw_message_from_chat_response(self, response: ChatResponse) -> Any:
    """Extract raw message from a chat response."""

    raw = response.raw
    if raw is None:
        raise MissingRawChatResponseError("ChatResponse is missing raw attribute.")

    try:
        msg = raw.choices[0].message
        return msg
    except (AttributeError, IndexError, TypeError):
        raise RawMessageExtractionError(
            "Could not extract message from chat response raw attribute."
        )

get_reasoning_from_chat_response(response)

Extract reasoning from a chat response.

Source code in src/kibad_llm/llms/base.py
78
79
80
81
82
def get_reasoning_from_chat_response(self, response: ChatResponse) -> str | None:
    """Extract reasoning from a chat response."""
    raise NotImplementedError(
        f"get_reasoning_from_chat_response() is not implemented for {type(self)}"
    )

get_response_content_from_chat_response(response)

Extract content from chat response.

Source code in src/kibad_llm/llms/base.py
84
85
86
87
88
89
90
91
92
def get_response_content_from_chat_response(self, response: ChatResponse) -> str:
    """Extract content from chat response."""

    response_content = response.message.content
    if response_content is None:
        raise MissingResponseContentError("LLM response is missing content.")
    if not response_content.strip():
        raise EmptyResponseMessageError("LLM returned an empty message.")
    return response_content