This function is often used when creating custom tasks for processing data with structured output. You’ll see it in action when we cover custom task creation in a future guide.
import asynciofrom typing import Listfrom pydantic import BaseModelfrom cognee.infrastructure.llm.LLMGateway import LLMGatewayclass MiniEntity(BaseModel): name: str type: strclass MiniGraph(BaseModel): nodes: List[MiniEntity]async def main(): system_prompt = ( "Extract entities as nodes with name and type. " "Use concise, literal values present in the text." ) text = "Apple develops iPhone; Audi produces the R8." result = await LLMGateway.acreate_structured_output(text, system_prompt, MiniGraph) print(result) # MiniGraph(nodes=[MiniEntity(name='Apple', type='Organization'), ...])if __name__ == "__main__": asyncio.run(main())
This simple example uses a basic schema for demonstration. In practice, you can define complex Pydantic models with nested structures, validation rules, and custom types.