Skip to content

Utils

Defines utilities for gllm_inference.

SizeUnit

Bases: IntEnum

Defines the valid size units.

base64_to_bytes(value, *, allowed_mimetypes=DEFAULT_BASE64_ALLOWED_MIMETYPES)

Decode a base64 string to bytes based on allowed MIME type.

The conversion steps are as follows
  1. The function first attempts to decode the given string from base64.
  2. If decoding succeeds, it checks the MIME type of the decoded content.
  3. When the MIME type matches one of the allowed patterns (e.g., "image/*"), the raw bytes are returned. Otherwise, the original string is returned unchanged.

Parameters:

Name Type Description Default
value str

Input data to decode.

required
allowed_mimetypes tuple[str, ...]

MIME type prefixes that are allowed to be decoded into bytes. Defaults to ("image/", "audio/", "video/*").

DEFAULT_BASE64_ALLOWED_MIMETYPES

Returns:

Type Description
str | bytes

str | bytes: Base64-encoded string or raw bytes if MIME type is allowed; otherwise returns original string.

Raises:

Type Description
ValueError

If the input is not a string.

build_google_auth_params(api_key, credentials_path, credentials_info, project_id, location)

Build Google client authentication parameters based on the provided credentials.

Parameters:

Name Type Description Default
api_key str | None

Required for Google Gen AI authentication. Cannot be used with credentials_path or credentials_info.

required
credentials_path str | None

For Google Vertex AI authentication. Path to the service account credentials JSON file. Cannot be used together with api_key or credentials_info.

required
credentials_info dict[str, Any] | None

For Google Vertex AI authentication. Service account credentials JSON contents. Cannot be used with api_key or credentials_path.

required
project_id str | None

The Google Vertex AI project ID.

required
location str | None

The Google Vertex AI location.

required

Returns:

Type Description
dict[str, Any]

dict[str, Any]: The Google Gen AI and Vertex AI authentication parameters.

Raises:

Type Description
ValueError

If more than one of api_key, credentials_path, or credentials_info is provided.

build_openai_embedded_error(message, code, status_code=None)

Build a raisable error from an OpenAI-compatible embedded provider error payload.

Parameters:

Name Type Description Default
message str | None

The provider-supplied error message, if any.

required
code Any

The provider-supplied error code.

required
status_code Any

The provider-supplied HTTP status code, when reported separately from code. A numeric code is used as the fallback.

None

Returns:

Name Type Description
Exception Exception

The error to raise so the translator can classify it.

coerce_openai_status_code(value)

Coerce a provider-supplied code or status code to an int HTTP status code.

Parameters:

Name Type Description Default
value Any

An int, a digit string, or anything else.

required

Returns:

Type Description
int | None

int | None: The int form of a numeric value, None otherwise.

get_openai_model_id_path(base_url)

Get ModelId path for OpenAI-compatible endpoints.

Returns None when base_url points to the default OpenAI endpoint, otherwise returns the provided base_url value.

Parameters:

Name Type Description Default
base_url str

The base URL of the OpenAI-compatible endpoint.

required

Returns:

Type Description
str | None

str | None: The ModelId path for OpenAI-compatible endpoints.

get_size(obj, unit=SizeUnit.BYTES)

Get the pickle-serialized size of an object in the given unit.

Note

This measures the size of the object when serialized using pickle, NOT its in-memory footprint.

Parameters:

Name Type Description Default
obj Any

The object to get the size of.

required
unit SizeUnit

The unit to get the size in. Defaults to SizeUnit.BYTES.

BYTES

Returns:

Name Type Description
float float

The size of the object in the given mode.

is_valid_url(text)

Check if a URL is valid.

This method validate URL format and rejects dangerous schemes and private/local addresses.

Parameters:

Name Type Description Default
text str

The URL string to validate.

required

Returns:

Name Type Description
bool bool

True if URL is valid, False otherwise.

load_langchain_model(model_class_path, model_name, model_kwargs)

Loads the LangChain's model instance.

Parameters:

Name Type Description Default
model_class_path str

The path to the LangChain's class, e.g. "langchain_openai.ChatOpenAI".

required
model_name str

The model name.

required
model_kwargs dict[str, Any]

The additional keyword arguments.

required

Returns:

Type Description
BaseChatModel | Embeddings

BaseChatModel | Embeddings: The LangChain's model instance.

parse_model_data(model)

Parses the model data from LangChain's BaseChatModel or Embeddings instance.

Parameters:

Name Type Description Default
model BaseChatModel | Embeddings

The LangChain's BaseChatModel or Embeddings instance.

required

Returns:

Type Description
dict[str, str]

dict[str, str]: The dictionary containing the model name and path.

Raises:

Type Description
ValueError

If the model name is not found in the model data.

validate_os(supported_os, module_name)

Validate if the current system is supported.

Parameters:

Name Type Description Default
supported_os str | tuple[str, ...]

The supported operating system(s).

required
module_name str

The name of the module to validate.

required

Raises:

Type Description
OSError

If the current operating system is not supported.

validate_trace_attributes(trace_attributes, logger)

Return OpenTelemetry-compatible scalar trace attributes.

Parameters:

Name Type Description Default
trace_attributes dict[str, Any] | None

User-provided trace attributes.

required
logger Logger

Logger used to report skipped attributes.

required

Returns:

Type Description
dict[str, TraceValue]

dict[str, TraceValue]: The supported trace attributes.