Response Parsing

Response parsing utilities for LLM structured outputs.

Provides generic parsing and validation for structured responses from language models.

Functions

arshai.llms.utils.response_parsing.convert_typeddict_to_basemodel(typeddict_class)[source]

Convert a TypedDict class to a BaseModel class for Azure OpenAI SDK compatibility.

Parameters:

typeddict_class (Type) – TypedDict class to convert

Return type:

Type[BaseModel]

Returns:

Dynamically created BaseModel class with same fields and types

arshai.llms.utils.response_parsing.extract_text_from_structure(instance, text_fields=None)[source]

Extract text content from a structured response for pattern matching.

Parameters:
  • instance (object) – The structured response instance

  • text_fields (list) – List of field names to extract text from (None for all string fields)

Return type:

str

Returns:

Combined text content from specified fields

arshai.llms.utils.response_parsing.parse_to_structure(content, structure_type)[source]

Parse response content into the specified structure type.

Parameters:
  • content (Union[str, dict]) – Response content to parse (string or dict)

  • structure_type (Type[TypeVar(T)]) – Target Pydantic model class or structure type

Return type:

TypeVar(T)

Returns:

Instance of the structure type

Raises:

ValueError – If parsing fails or content doesn’t match structure

arshai.llms.utils.response_parsing.validate_structure_fields(instance, required_fields)[source]

Validate that a structured response has all required fields.

Parameters:
  • instance (object) – The structured response instance

  • required_fields (list) – List of required field names

Return type:

bool

Returns:

True if all required fields are present and non-empty