Skip to content

Responses

Responses

Warning

This API is experimental and subject to changes based upon our experience as we integrate additional providers. Use with caution.

any_llm.responses(model, input_data, *, tools=None, tool_choice=None, max_output_tokens=None, temperature=None, top_p=None, stream=None, api_key=None, api_base=None, timeout=None, user=None, **kwargs)

Create a response using the OpenAI-style Responses API.

This follows the OpenAI Responses API shape and returns the aliased any_llm.types.responses.Response type. If stream=True, an iterator of any_llm.types.responses.ResponseStreamEvent items is returned.

Parameters:

Name Type Description Default
model str

Model identifier in format 'provider/model' (e.g., 'openai/gpt-4o')

required
input_data Any

The input payload accepted by provider's Responses API. For OpenAI-compatible providers, this is typically a list mixing text, images, and tool instructions, or a dict per OpenAI spec.

required
tools Optional[List[Union[dict[str, Any], Callable[..., Any]]]]

Optional tools for tool calling (Python callables or OpenAI tool dicts)

None
tool_choice Optional[Union[str, dict[str, Any]]]

Controls which tools the model can call

None
max_output_tokens Optional[int]

Maximum number of output tokens to generate

None
temperature Optional[float]

Controls randomness in the response (0.0 to 2.0)

None
top_p Optional[float]

Controls diversity via nucleus sampling (0.0 to 1.0)

None
stream Optional[bool]

Whether to stream response events

None
api_key Optional[str]

API key for the provider

None
api_base Optional[str]

Base URL for the provider API

None
timeout Optional[Union[float, int]]

Request timeout in seconds

None
user Optional[str]

Unique identifier for the end user

None
**kwargs Any

Additional provider-specific parameters

{}

Returns:

Type Description
Response | Iterator[ResponseStreamEvent]

Either a Response object (non-streaming) or an iterator of

Response | Iterator[ResponseStreamEvent]

ResponseStreamEvent (streaming).

Raises:

Type Description
NotImplementedError

If the selected provider does not support the Responses API.

Source code in src/any_llm/api.py
def responses(
    model: str,
    input_data: Any,
    *,
    tools: Optional[List[Union[dict[str, Any], Callable[..., Any]]]] = None,
    tool_choice: Optional[Union[str, dict[str, Any]]] = None,
    max_output_tokens: Optional[int] = None,
    temperature: Optional[float] = None,
    top_p: Optional[float] = None,
    stream: Optional[bool] = None,
    api_key: Optional[str] = None,
    api_base: Optional[str] = None,
    timeout: Optional[Union[float, int]] = None,
    user: Optional[str] = None,
    **kwargs: Any,
) -> Response | Iterator[ResponseStreamEvent]:
    """Create a response using the OpenAI-style Responses API.

    This follows the OpenAI Responses API shape and returns the aliased
    `any_llm.types.responses.Response` type. If `stream=True`, an iterator of
    `any_llm.types.responses.ResponseStreamEvent` items is returned.

    Args:
        model: Model identifier in format 'provider/model' (e.g., 'openai/gpt-4o')
        input_data: The input payload accepted by provider's Responses API.
            For OpenAI-compatible providers, this is typically a list mixing
            text, images, and tool instructions, or a dict per OpenAI spec.
        tools: Optional tools for tool calling (Python callables or OpenAI tool dicts)
        tool_choice: Controls which tools the model can call
        max_output_tokens: Maximum number of output tokens to generate
        temperature: Controls randomness in the response (0.0 to 2.0)
        top_p: Controls diversity via nucleus sampling (0.0 to 1.0)
        stream: Whether to stream response events
        api_key: API key for the provider
        api_base: Base URL for the provider API
        timeout: Request timeout in seconds
        user: Unique identifier for the end user
        **kwargs: Additional provider-specific parameters

    Returns:
        Either a `Response` object (non-streaming) or an iterator of
        `ResponseStreamEvent` (streaming).

    Raises:
        NotImplementedError: If the selected provider does not support the Responses API.
    """
    provider_key, model_name = ProviderFactory.split_model_provider(model)

    config: dict[str, str] = {}
    if api_key:
        config["api_key"] = str(api_key)
    if api_base:
        config["api_base"] = str(api_base)
    api_config = ApiConfig(**config)

    provider = ProviderFactory.create_provider(provider_key, api_config)

    responses_kwargs = kwargs.copy()
    if tools is not None:
        responses_kwargs["tools"] = prepare_tools(tools)
    if tool_choice is not None:
        responses_kwargs["tool_choice"] = tool_choice
    if max_output_tokens is not None:
        responses_kwargs["max_output_tokens"] = max_output_tokens
    if temperature is not None:
        responses_kwargs["temperature"] = temperature
    if top_p is not None:
        responses_kwargs["top_p"] = top_p
    if stream is not None:
        responses_kwargs["stream"] = stream
    if timeout is not None:
        responses_kwargs["timeout"] = timeout
    if user is not None:
        responses_kwargs["user"] = user

    return provider.responses(model_name, input_data, **responses_kwargs)

any_llm.aresponses(model, input_data, *, tools=None, tool_choice=None, max_output_tokens=None, temperature=None, top_p=None, stream=None, api_key=None, api_base=None, timeout=None, user=None, **kwargs) async

Create a response using the OpenAI-style Responses API.

This follows the OpenAI Responses API shape and returns the aliased any_llm.types.responses.Response type. If stream=True, an iterator of any_llm.types.responses.ResponseStreamEvent items is returned.

Parameters:

Name Type Description Default
model str

Model identifier in format 'provider/model' (e.g., 'openai/gpt-4o')

required
input_data Any

The input payload accepted by provider's Responses API. For OpenAI-compatible providers, this is typically a list mixing text, images, and tool instructions, or a dict per OpenAI spec.

required
tools Optional[List[Union[dict[str, Any], Callable[..., Any]]]]

Optional tools for tool calling (Python callables or OpenAI tool dicts)

None
tool_choice Optional[Union[str, dict[str, Any]]]

Controls which tools the model can call

None
max_output_tokens Optional[int]

Maximum number of output tokens to generate

None
temperature Optional[float]

Controls randomness in the response (0.0 to 2.0)

None
top_p Optional[float]

Controls diversity via nucleus sampling (0.0 to 1.0)

None
stream Optional[bool]

Whether to stream response events

None
api_key Optional[str]

API key for the provider

None
api_base Optional[str]

Base URL for the provider API

None
timeout Optional[Union[float, int]]

Request timeout in seconds

None
user Optional[str]

Unique identifier for the end user

None
**kwargs Any

Additional provider-specific parameters

{}

Returns:

Type Description
Response | Iterator[ResponseStreamEvent]

Either a Response object (non-streaming) or an iterator of

Response | Iterator[ResponseStreamEvent]

ResponseStreamEvent (streaming).

Raises:

Type Description
NotImplementedError

If the selected provider does not support the Responses API.

Source code in src/any_llm/api.py
async def aresponses(
    model: str,
    input_data: Any,
    *,
    tools: Optional[List[Union[dict[str, Any], Callable[..., Any]]]] = None,
    tool_choice: Optional[Union[str, dict[str, Any]]] = None,
    max_output_tokens: Optional[int] = None,
    temperature: Optional[float] = None,
    top_p: Optional[float] = None,
    stream: Optional[bool] = None,
    api_key: Optional[str] = None,
    api_base: Optional[str] = None,
    timeout: Optional[Union[float, int]] = None,
    user: Optional[str] = None,
    **kwargs: Any,
) -> Response | Iterator[ResponseStreamEvent]:
    """Create a response using the OpenAI-style Responses API.

    This follows the OpenAI Responses API shape and returns the aliased
    `any_llm.types.responses.Response` type. If `stream=True`, an iterator of
    `any_llm.types.responses.ResponseStreamEvent` items is returned.

    Args:
        model: Model identifier in format 'provider/model' (e.g., 'openai/gpt-4o')
        input_data: The input payload accepted by provider's Responses API.
            For OpenAI-compatible providers, this is typically a list mixing
            text, images, and tool instructions, or a dict per OpenAI spec.
        tools: Optional tools for tool calling (Python callables or OpenAI tool dicts)
        tool_choice: Controls which tools the model can call
        max_output_tokens: Maximum number of output tokens to generate
        temperature: Controls randomness in the response (0.0 to 2.0)
        top_p: Controls diversity via nucleus sampling (0.0 to 1.0)
        stream: Whether to stream response events
        api_key: API key for the provider
        api_base: Base URL for the provider API
        timeout: Request timeout in seconds
        user: Unique identifier for the end user
        **kwargs: Additional provider-specific parameters

    Returns:
        Either a `Response` object (non-streaming) or an iterator of
        `ResponseStreamEvent` (streaming).

    Raises:
        NotImplementedError: If the selected provider does not support the Responses API.
    """
    provider_key, model_name = ProviderFactory.split_model_provider(model)

    config: dict[str, str] = {}
    if api_key:
        config["api_key"] = str(api_key)
    if api_base:
        config["api_base"] = str(api_base)
    api_config = ApiConfig(**config)

    provider = ProviderFactory.create_provider(provider_key, api_config)

    responses_kwargs = kwargs.copy()
    if tools is not None:
        responses_kwargs["tools"] = prepare_tools(tools)
    if tool_choice is not None:
        responses_kwargs["tool_choice"] = tool_choice
    if max_output_tokens is not None:
        responses_kwargs["max_output_tokens"] = max_output_tokens
    if temperature is not None:
        responses_kwargs["temperature"] = temperature
    if top_p is not None:
        responses_kwargs["top_p"] = top_p
    if stream is not None:
        responses_kwargs["stream"] = stream
    if timeout is not None:
        responses_kwargs["timeout"] = timeout
    if user is not None:
        responses_kwargs["user"] = user

    return await provider.aresponses(model_name, input_data, **responses_kwargs)