Skip to content

Query Transformer

Modules concerning the query transformers used in Gen AI applications.

ManyToOneQueryTransformer(lm_request_processor=None, extract_func=None, combine_func=None, on_error=ErrorHandling.RAISE, *, lm_invoker=None, fallback_lms=None)

Bases: BaseQueryTransformer, LMComponent

A query transformer that takes multiple queries and outputs a single transformed query.

Examples:

```python import asyncio

from gllm_inference.lm_invoker import build_lm_invoker from gllm_retrieval.query_transformer import ManyToOneQueryTransformer

lm_invoker = build_lm_invoker(model_id="openai/gpt-5-nano").prompt.build( system_template="Rewrite the following query. Only output the transformed query.", user_template="Query: {query}", )

transformer = ManyToOneQueryTransformer(lm_invoker=lm_invoker)

multiple_input = [ "Find recent research on diffusion transformers.", "Find recent research on diffusion transformers." ] result = asyncio.run(transformer.transform(multiple_input, return_str=True)) print(result) # Result is a plain string.

```

Attributes: lm_invoker (BaseLMInvoker): The LM invoker to use for transforming queries. fallback_lms (list[BaseLMInvoker]): The fallback LM invokers to use for transforming queries. extract_func (Callable[[Queries | dict[str, Queries]], Queries]): A function to extract the transformed query from the LM output. This transformer's logic will ensure that effectively one string is obtained per input query. combine_func (Callable[[list[str]], str]): A function to combine multiple input queries into a single string before sending to the LM. Defaults to _default_combine_func. on_error (ErrorHandling): The error handling strategy. Defaults to ErrorHandling.RAISE. supports_single_output (bool): Always True for this transformer.

Initialize the ManyToOneTransformer.

Parameters:

Name Type Description Default
lm_request_processor LMRequestProcessor | None

Legacy LM request processor. Either this or lm_invoker must be provided. Defaults to None.

None
extract_func Callable[[Queries | dict[str, Queries]], Queries] | None

Function to extract the transformed query. Defaults to base class's default.

None
combine_func Callable[[list[str]], str] | None

Function to combine input queries. Defaults to _default_combine_func.

None
on_error ErrorHandling

Error handling strategy. Defaults to ErrorHandling.RAISE.

RAISE
lm_invoker BaseLMInvoker | None

The LM invoker for transforming queries. Keyword-only. Defaults to None.

None
fallback_lms list[BaseLMInvoker] | None

Ordered fallback LM invokers. Keyword-only. Defaults to None.

None

NoOpQueryTransformer(on_error=ErrorHandling.RAISE)

Bases: BaseQueryTransformer

A query transformer that returns the input query without modification.

Examples:

import asyncio

from gllm_retrieval.query_transformer import NoOpQueryTransformer

transformer = NoOpQueryTransformer()

single_input = "Find recent research on diffusion transformers."

result = asyncio.run(transformer.transform(single_input))
print(result)

Attributes:

Name Type Description
on_error ErrorHandling

The error handling strategy. Since this transformer performs no actual transformation that can fail, this primarily exists for API consistency. Defaults to ErrorHandling.RAISE.

Initialize the NoOpQueryTransformer.

Parameters:

Name Type Description Default
on_error ErrorHandling

The error handling strategy. Defaults to ErrorHandling.RAISE.

RAISE

from_lm_components(*args, **kwargs) classmethod

Create a NoOpQueryTransformer instance.

Returns:

Name Type Description
NoOpQueryTransformer NoOpQueryTransformer

A new instance of NoOpQueryTransformer.

OneToManyQueryTransformer(lm_request_processor=None, extract_func=None, on_error=ErrorHandling.RAISE, *, lm_invoker=None, fallback_lms=None)

Bases: BaseQueryTransformer, LMComponent

A query transformer that takes one query and outputs one or more transformed queries.

When processing multiple input queries (a list of queries), the results from each query's transformation (which itself can be multiple queries) are flattened into a single list of transformed queries.

Examples:

import asyncio

from gllm_inference.lm_invoker import build_lm_invoker
from gllm_retrieval.query_transformer import OneToManyQueryTransformer

lm_invoker = build_lm_invoker(model_id="openai/gpt-5-nano").prompt.build(
    system_template="Rewrite the following query. Only output the transformed query.",
    user_template="Query: {query}",
)

transformer = OneToManyQueryTransformer(lm_invoker=lm_invoker)

single_input = "Find recent research on diffusion transformers."
result = asyncio.run(transformer.transform(single_input))
print(result)

Attributes:

Name Type Description
lm_invoker BaseLMInvoker

The LM invoker to use for transforming queries.

fallback_lms list[BaseLMInvoker]

The fallback LM invokers to use for transforming queries.

extract_func Callable[[Queries | dict[str, Queries]], Queries]

A function to extract the list of transformed queries from the LM output. If None, _default_extract_func is used.

on_error ErrorHandling

The error handling strategy. Defaults to ErrorHandling.RAISE.

supports_single_output bool

Always False for this transformer; return_str=True is not supported.

Initialize the OneToManyQueryTransformer.

Parameters:

Name Type Description Default
lm_request_processor LMRequestProcessor | None

Legacy LM request processor. Either this or lm_invoker must be provided. Defaults to None.

None
extract_func Callable[[Queries | dict[str, Queries]], Queries]

Function to extract transformed queries. Defaults to _default_extract_func.

None
on_error ErrorHandling

Error handling strategy. Defaults to ErrorHandling.RAISE.

RAISE
lm_invoker BaseLMInvoker | None

The LM invoker for transforming queries. Keyword-only. Defaults to None.

None
fallback_lms list[BaseLMInvoker] | None

Ordered fallback LM invokers. Keyword-only. Defaults to None.

None

OneToOneQueryTransformer(lm_request_processor=None, extract_func=None, on_error=ErrorHandling.RAISE, *, lm_invoker=None, fallback_lms=None)

Bases: BaseQueryTransformer, LMComponent

A query transformer that processes each input query to produce one corresponding transformed query.

If a list of queries is provided as input, each query is processed independently, resulting in a list of transformed queries (maintaining the "one-to-one" mapping for each item). Pass return_str=True to transform to receive the result as a plain string instead of a list. When a list of multiple queries is provided with return_str=True, only the first transformed result is returned and the rest are discarded.

Examples:

```python import asyncio

from gllm_inference.lm_invoker import build_lm_invoker from gllm_retrieval.query_transformer import OneToOneQueryTransformer

lm_invoker = build_lm_invoker(model_id="openai/gpt-5-nano").prompt.build( system_template="Rewrite the following query. Only output the transformed query.", user_template="Query: {query}", )

transformer = OneToOneQueryTransformer(lm_invoker=lm_invoker)

single_input = "Find recent research on diffusion transformers." result = asyncio.run(transformer.transform(single_input, return_str=True)) print(result) # Result is a plain string.

```

Attributes: lm_invoker (BaseLMInvoker): The LM invoker to use for transforming queries. fallback_lms (list[BaseLMInvoker]): The fallback LM invokers to use for transforming queries. extract_func (Callable[[Queries | dict[str, Queries]], Queries]): A function to extract the transformed query from the LM output. This transformer's logic will ensure that effectively one string is obtained per input query. on_error (ErrorHandling): The error handling strategy to use when an exception occurs during query transformation. Defaults to ErrorHandling.RAISE. supports_single_output (bool): Always True for this transformer.

Initialize the OneToOneQueryTransformer.

Parameters:

Name Type Description Default
lm_request_processor LMRequestProcessor | None

Legacy LM request processor. Either this or lm_invoker must be provided. Defaults to None.

None
extract_func Callable[[Queries | dict[str, Queries]], Queries] | None

A function to extract the transformed query from the LM output. If None, the base class's default extractor is used. The logic within this transformer ensures that a single string query is derived for each input query.

None
on_error ErrorHandling

The error handling strategy to use. Defaults to ErrorHandling.RAISE.

RAISE
lm_invoker BaseLMInvoker | None

The LM invoker for transforming queries. Keyword-only. Defaults to None.

None
fallback_lms list[BaseLMInvoker] | None

Ordered fallback LM invokers. Keyword-only. Defaults to None.

None