Skip to main content

_StreamLogger

Base for stream logging wrappers.

init

_collect

_log

_SyncStreamWrapper

Sync stream wrapper with logging.

init

iter

next

enter

exit

del

_AsyncStreamWrapper

Async stream wrapper with logging.

init

aiter

del

ModelBackendMeta

Metaclass that automatically preprocesses messages in run method. Automatically wraps the run method of any class inheriting from BaseModelBackend to preprocess messages (remove <think> tags) before they are sent to the model.

new

Wraps run method with preprocessing if it exists in the class.

BaseModelBackend

Base class for different model backends. It may be OpenAI API, a local LLM, a stub for unit tests, etc. Parameters:
  • model_type (Union[ModelType, str]): Model for which a backend is created.
  • model_config_dict (Optional[Dict[str, Any]], optional): A config dictionary. (default: :obj:{})
  • api_key (Optional[str], optional): The API key for authenticating with the model service. (default: :obj:None)
  • url (Optional[str], optional): The url to the model service. (default: :obj:None)
  • token_counter (Optional[BaseTokenCounter], optional): Token counter to use for the model. If not provided, :obj:OpenAITokenCounter will be used. (default: :obj:None)
  • timeout (Optional[float], optional): The timeout value in seconds for API calls. (default: :obj:None)
  • max_retries (int, optional): Maximum number of retries for API calls. (default: :obj:3)

init

token_counter

Returns: BaseTokenCounter: The token counter following the model’s tokenization style.

_prepare_request_config

Prepare the request configuration dictionary. Creates a deep copy of the model config and handles tool-related parameters. If no tools are specified, removes parallel_tool_calls as OpenAI API only allows it when tools are present. Parameters:
  • tools (Optional[List[Dict[str, Any]]]): The tools to include in the request. (default: :obj:None)
Returns: Dict[str, Any]: The prepared request configuration.

preprocess_messages

Preprocess messages before sending to model API. Removes thinking content from assistant and user messages. Automatically formats messages for parallel tool calls if tools are detected. Parameters:
  • messages (List[OpenAIMessage]): Original messages.
Returns: List[OpenAIMessage]: Preprocessed messages

_log_request

Log the request messages to a JSON file if logging is enabled. Parameters:
  • messages (List[OpenAIMessage]): The messages to log.
Returns: Optional[str]: The path to the log file if logging is enabled, None otherwise.

_log_response

Log the response to the existing log file. Parameters:
  • log_path (str): The path to the log file.
  • response (Any): The response to log.

_log_and_trace

Update Langfuse trace with session metadata. This method updates the current Langfuse trace with agent session information and model metadata. Called at the start of _run() and _arun() methods before API execution.

_run

Runs the query to the backend model in a non-stream mode. Parameters:
  • messages (List[OpenAIMessage]): Message list with the chat history in OpenAI API format.
  • response_format (Optional[Type[BaseModel]]): The format of the response.
  • tools (Optional[List[Dict[str, Any]]]): The schema of the tools to use for the request.
Returns: Union[ChatCompletion, Stream[ChatCompletionChunk], Any]: ChatCompletion in the non-stream mode, or Stream[ChatCompletionChunk] in the stream mode, or ChatCompletionStreamManager[BaseModel] in the structured stream mode.

run

Runs the query to the backend model. Parameters:
  • messages (List[OpenAIMessage]): Message list with the chat history in OpenAI API format.
  • response_format (Optional[Type[BaseModel]]): The response format to use for the model. (default: :obj:None)
  • tools (Optional[List[Tool]]): The schema of tools to use for the model for this request. Will override the tools specified in the model configuration (but not change the configuration). (default: :obj:None)
Returns: Union[ChatCompletion, Stream[ChatCompletionChunk], Any]: ChatCompletion in the non-stream mode, Stream[ChatCompletionChunk] in the stream mode, or ChatCompletionStreamManager[BaseModel] in the structured stream mode.

count_tokens_from_messages

Count the number of tokens in the messages using the specific tokenizer. Parameters:
  • messages (List[Dict]): message list with the chat history in OpenAI API format.
Returns: int: Number of tokens in the messages.

_to_chat_completion

token_limit

Returns: int: The maximum token limit for the given model.

stream

Returns: bool: Whether the model is in stream mode.