Skip to content

Models: Base

See also

  • Guide → Providers: guide/providers.md
  • Architecture → Provider Abstraction: architecture/provider-abstraction.md

alloy.models.base

ModelBackend

Abstract provider interface.

Concrete backends implement completion and tool-calling behavior.

run_stream_loop

run_stream_loop(state, stream_step)

Drive a streaming conversation that may invoke tools.

stream_step must return a generator that yields text chunks. When the generator finishes, it should expose a callable attribute _alloy_get_tool_calls returning the tool calls gathered for that turn (or None/[] if no calls). When no tool calls are returned, iteration stops. Otherwise tool results are executed and appended via the state before the next turn.

should_finalize_structured_output

should_finalize_structured_output(text, schema)

Return True if a finalize turn should be attempted for typed outputs.

Rules: - If no schema, do not finalize. - If schema (or its wrapped form) represents a string, finalize only if the current text is empty. Non-empty text is considered final for strings. - For object schemas, attempt to parse JSON and ensure required keys exist; if parsing fails or required keys are missing, request a finalize turn. - Otherwise, finalize when the current text is empty or not valid JSON (after stripping optional code fences).

serialize_tool_payload

serialize_tool_payload(payload)

Serialize a tool's return value into a JSON string (or pass through string).

Uses to_jsonable to normalize complex types before encoding. Falls back to str(payload) if encoding fails.

ensure_object_schema

ensure_object_schema(schema)

Return a JSON Schema object, wrapping primitives as {"value": }.

If schema is None or not a dict, returns None.