from langchain_core.runnables import RunnableLambda
from pydantic import BaseModel, ConfigDict

CHAPTERS = {
    "Python and Roots": 7,
    "Python and Branches": 8,
}

class ChapterRequest(BaseModel):
    model_config = ConfigDict(strict=True, extra="forbid")
    title: str

def find_chapter(data):
    request = ChapterRequest.model_validate(data)
    count = CHAPTERS.get(request.title)
    if count is None:
        return {"found": False, "title": request.title}
    return {
        "found": True,
        "title": request.title,
        "lessons": count,
    }

def describe_chapter(record):
    if not record["found"]:
        return f"Not found: {record['title']}"
    return f"{record['title']}: {record['lessons']} lessons"

find = RunnableLambda(find_chapter)
describe = RunnableLambda(describe_chapter)
workflow = find | describe

print(workflow.invoke({"title": "Python and Roots"}))
