Skip to content

Index

BaseOutputParser #

Bases: DispatcherSpanMixin, ABC

Output parser class.

Source code in llama-index-core/llama_index/core/types.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class BaseOutputParser(DispatcherSpanMixin, ABC):
    """Output parser class."""

    @abstractmethod
    def parse(self, output: str) -> Any:
        """Parse, validate, and correct errors programmatically."""

    def format(self, query: str) -> str:
        """Format a query with structured output formatting instructions."""
        return query

    def format_messages(self, messages: List[ChatMessage]) -> List[ChatMessage]:
        """Format a list of messages with structured output formatting instructions."""
        # NOTE: apply output parser to either the first message if it's a system message
        #       or the last message
        if messages:
            if messages[0].role == MessageRole.SYSTEM:
                messages[0].content = self.format(messages[0].content or "")
            else:
                messages[-1].content = self.format(messages[-1].content or "")

        return messages

    @classmethod
    def __get_pydantic_core_schema__(
        cls, source: Type[Any], handler: GetCoreSchemaHandler
    ) -> CoreSchema:
        return core_schema.any_schema()

    @classmethod
    def __get_pydantic_json_schema__(
        cls, core_schema: CoreSchema, handler: GetJsonSchemaHandler
    ) -> Dict[str, Any]:
        json_schema = handler(core_schema)
        return handler.resolve_ref_schema(json_schema)

parse abstractmethod #

parse(output: str) -> Any

Parse, validate, and correct errors programmatically.

Source code in llama-index-core/llama_index/core/types.py
42
43
44
@abstractmethod
def parse(self, output: str) -> Any:
    """Parse, validate, and correct errors programmatically."""

format #

format(query: str) -> str

Format a query with structured output formatting instructions.

Source code in llama-index-core/llama_index/core/types.py
46
47
48
def format(self, query: str) -> str:
    """Format a query with structured output formatting instructions."""
    return query

format_messages #

format_messages(messages: List[ChatMessage]) -> List[ChatMessage]

Format a list of messages with structured output formatting instructions.

Source code in llama-index-core/llama_index/core/types.py
50
51
52
53
54
55
56
57
58
59
60
def format_messages(self, messages: List[ChatMessage]) -> List[ChatMessage]:
    """Format a list of messages with structured output formatting instructions."""
    # NOTE: apply output parser to either the first message if it's a system message
    #       or the last message
    if messages:
        if messages[0].role == MessageRole.SYSTEM:
            messages[0].content = self.format(messages[0].content or "")
        else:
            messages[-1].content = self.format(messages[-1].content or "")

    return messages