Response synthesizer
Defines a base class for response synthesizers used in Gen AI applications.
ResponseSynthesizer(strategy, streamable=True)
Bases: Component
A response synthesizer that uses a strategy to synthesize the response.
Attributes:
| Name | Type | Description |
|---|---|---|
strategy |
BaseSynthesisStrategy
|
The strategy used to synthesize the response. |
streamable |
bool
|
A flag to indicate whether the synthesized response will be streamed if an event emitter is provided. |
preset |
PresetFactory
|
Factory for creating ResponseSynthesizer instances with preset configurations. |
Initializes a new instance of the BaseResponseSynthesizer class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
BaseSynthesisStrategy
|
The strategy used to synthesize the response. |
required |
streamable
|
bool
|
A flag to indicate whether the synthesized response will be streamed if an event emitter is provided. Defaults to True. |
True
|
map_reduce(map_lm_request_processor=None, reduce_lm_request_processor=None, chunks_repacker=None, extractor_func=None, batch_size=1, max_iterations=1, streamable=True, map_lm_invoker=None, reduce_lm_invoker=None, map_fallback_lms=None, reduce_fallback_lms=None)
classmethod
Builds a ResponseSynthesizer backed by MapReduceSynthesisStrategy.
Examples:
map_invoker = build_lm_invoker(model_id="openai/gpt-5-nano")
map_invoker.prompt.build(system_template=..., user_template=...)
reduce_invoker = build_lm_invoker(model_id="openai/gpt-5-nano")
reduce_invoker.prompt.build(system_template=..., user_template=...)
synthesizer = ResponseSynthesizer.map_reduce(
map_lm_invoker=map_invoker, reduce_lm_invoker=reduce_invoker
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
map_lm_request_processor
|
LMRequestProcessor | None
|
The deprecated map-phase request processor. Defaults to None. |
None
|
reduce_lm_request_processor
|
LMRequestProcessor | None
|
The deprecated reduce-phase request processor. Defaults to None. |
None
|
chunks_repacker
|
Repacker | None
|
The repacker used to batch chunks. Defaults to None. |
None
|
extractor_func
|
Callable[[LMOutput], Any] | None
|
The function used to extract the final result from the LM output. Defaults to None. |
None
|
batch_size
|
int
|
The number of chunks mapped per batch. Defaults to 1. |
1
|
max_iterations
|
int
|
The maximum number of reduce iterations. Defaults to 1. |
1
|
streamable
|
bool
|
Whether the synthesizer streams output. Defaults to True. |
True
|
map_lm_invoker
|
BaseLMInvoker | None
|
The map-phase invoker to construct this strategy with. Defaults to None. |
None
|
reduce_lm_invoker
|
BaseLMInvoker | None
|
The reduce-phase invoker to construct this strategy with. Defaults to None. |
None
|
map_fallback_lms
|
list[BaseLMInvoker] | None
|
The fallback invokers to use if map_lm_invoker fails. Defaults to None. |
None
|
reduce_fallback_lms
|
list[BaseLMInvoker] | None
|
The fallback invokers to use if reduce_lm_invoker fails. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ResponseSynthesizer |
ResponseSynthesizer
|
The constructed synthesizer. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Validation is delegated to MapReduceSynthesisStrategy. |
refine(lm_request_processor=None, batch_size=1, extractor_func=None, stream_drafts=False, streamable=True, lm_invoker=None, fallback_lms=None)
classmethod
Builds a ResponseSynthesizer with the refine strategy.
Examples:
lm_invoker = build_lm_invoker(model_id="openai/gpt-5-nano")
lm_invoker.prompt.build(system_template=..., user_template=...)
synthesizer = ResponseSynthesizer.refine(lm_invoker=lm_invoker, batch_size=2)
response = await synthesizer.synthesize(query=query, chunks=chunks)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lm_request_processor
|
LMRequestProcessor | None
|
The deprecated request processor to migrate from. Defaults to None. |
None
|
batch_size
|
int
|
The number of chunks refined per batch. Defaults to 1. |
1
|
extractor_func
|
Callable[[LMOutput], Any] | None
|
The function used to extract the final result from the LM output. Defaults to None. |
None
|
stream_drafts
|
bool
|
Whether to stream intermediate drafts. Defaults to False. |
False
|
streamable
|
bool
|
Whether the synthesizer streams output. Defaults to True. |
True
|
lm_invoker
|
BaseLMInvoker | None
|
The LM invoker to construct this synthesizer with, configured with a prompt builder. Defaults to None. |
None
|
fallback_lms
|
list[BaseLMInvoker] | None
|
The fallback invokers to use if lm_invoker fails. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ResponseSynthesizer |
ResponseSynthesizer
|
The constructed synthesizer. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither or both of lm_request_processor and lm_invoker are provided (raised by RefineSynthesisStrategy.init). |
static_list(format_response_func=None, streamable=True)
classmethod
Creates a response synthesizer with the static list strategy.
Examples:
synthesizer = ResponseSynthesizer.static_list()
response = await synthesizer.synthesize(chunks=chunks)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format_response_func
|
Callable[[list[str]], str] | None
|
A function that formats a list of context as a response. Defaults to None, in which case the default formatter function will be used. |
None
|
streamable
|
bool
|
A flag to indicate whether the synthesized response will be streamed if an event emitter is provided. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
ResponseSynthesizer |
ResponseSynthesizer
|
A response synthesizer with the static list strategy. |
stuff(lm_request_processor=None, chunks_repacker=None, extractor_func=None, streamable=True, lm_invoker=None, fallback_lms=None)
classmethod
Builds a ResponseSynthesizer backed by StuffSynthesisStrategy.
Examples:
lm_invoker = build_lm_invoker(model_id="openai/gpt-5-nano")
lm_invoker.prompt.build(system_template=..., user_template=...)
synthesizer = ResponseSynthesizer.stuff(lm_invoker=lm_invoker)
response = await synthesizer.synthesize(query=query, chunks=chunks)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lm_request_processor
|
LMRequestProcessor | None
|
The deprecated request processor to migrate from. Defaults to None. |
None
|
chunks_repacker
|
Repacker | None
|
The repacker used to combine chunks into a single context string. Defaults to None. |
None
|
extractor_func
|
Callable[[LMOutput], Any] | None
|
The function used to extract the final result from the LM output. Defaults to None. |
None
|
streamable
|
bool
|
Whether the synthesizer streams output. Defaults to True. |
True
|
lm_invoker
|
BaseLMInvoker | None
|
The LM invoker to construct this synthesizer with, configured with a prompt builder. Defaults to None. |
None
|
fallback_lms
|
list[BaseLMInvoker] | None
|
The fallback invokers to use if lm_invoker fails. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
ResponseSynthesizer |
ResponseSynthesizer
|
The constructed synthesizer. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither or both of lm_request_processor and lm_invoker are provided (raised by StuffSynthesisStrategy.init). |
synthesize(query=None, chunks=None, history=None, extra_contents=None, hyperparameters=None, event_emitter=None, max_lm_calls=5, **kwargs)
async
Synthesizes a response using the assigned strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str | None
|
The input query used to synthesize the response. Defaults to None. |
None
|
chunks
|
list[Chunk] | None
|
The list of chunks to be used as context. Defaults to None. |
None
|
history
|
list[Message] | None
|
The conversation history to be considered in generating the response. Defaults to None. |
None
|
extra_contents
|
list[MessageContent] | None
|
A list of extra contents to be included when generating the response. Defaults to None. |
None
|
hyperparameters
|
dict[str, Any] | None
|
The hyperparameters to be passed to the language model. Defaults to None. |
None
|
context_list
|
list[str] | None
|
The list of context to be included in the response. Defaults to None. |
required |
event_emitter
|
EventEmitter | None
|
The event emitter for handling events during response synthesis. Defaults to None. |
None
|
max_lm_calls
|
int
|
The max number of times the language model can be invoked. Defaults to 5. |
5
|
**kwargs
|
Any
|
Additional keyword arguments that will be passed to the strategy. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
The synthesized response. |