from lihil import Lihil, Route, EventStream, SSE, Paramfrom .api.param import UUID_PATTERNUUIDStr = Annotated[str, Param(min_length=36, pattern=UUID_PATTERN)]@messages.postasync def send_message_v1(session_id: UUIDStr,request: AgentRequest,agent: Annotated[IAgent, use(get_agent)],) -> EventStream:async_gen = agent.handle(request.content, user_id=request.user, session_id=session_id)yield SSE(event="open", data=await anext(async_gen))async for ans in async_gen:yield SSE(event="token", data=ans)yield SSE(event="done", data="")
Lihil is designed for simplicity without sacrificing power. Install with pip and start building your next Python web application.
from lihil import Lihil, HTTPException, Struct, Route, Param, Annotated, Emptyclass TodoItem(Struct):id: inttitle: strcompleted: bool = Falsetodo = Route("/todos", deps=[TodoRepo])PosInt = Annotated[int, Param(gt=0)]Todos = list[TodoItem]@todo.getasync def get_todos(todo_repo: TodoRepo, n: PosInt) -> Todos:return await todo_repo.list_todos()CREATED = Annotated[Empty, 201]@todo.postasync def create_todo(item: TodoItem, todo_repo: TodoRepo) -> CREATED:await todo_repo.add(item)if __name__ == "__main__":lhl = Lihil(todo)lhl.run(__file__)
使用msgspec自动解析和验证来自路径、查询、请求头和请求体的数据——比Pydantic快12倍,内存效率高25倍。
基于类型提示注入依赖。支持工厂模式、异步、作用域和单例——一切都快如闪电。
使用简洁、类型安全的API处理WebSocket连接。使用内置的WebSocket测试客户端轻松测试。
自动生成OpenAPI文档和问题详情。自定义异常转化为清晰的API响应。
内置JWT和OAuth2支持。认证对象类型安全且可序列化。
内置事件系统,高效地发布和处理进程内和跨进程事件。
内置端点、路由和中间件测试客户端——无需额外设置。
针对最小内存使用进行优化。减少GC开销,使服务在负载下更加稳定。
为AI而设计。内置SSE、MCP支持,远程处理器即将推出。
Lihil is backed by a growing community of Python developers. Get support, contribute, and help shape the future of fast Python web development.