Skip to content

Chapter 9 · Functional API and task replay ​

Existing Python workflows need not become explicit graphs. The Functional API places persistence boundaries around functions while retaining ordinary Python control flow.

Mechanics and design choices ​

@entrypoint defines the runnable workflow and @task wraps work whose result should be recorded. Tasks return futures; synchronous code reads .result(). Replay can reuse recorded results, but this does not make arbitrary external effects exactly-once. Put nondeterministic work inside task boundaries and still make external writes idempotent. Use a dictionary for multiple input fields.

Example ​

python
from langgraph.func import entrypoint, task
from langgraph.checkpoint.memory import InMemorySaver

@task
def normalize(text: str) -> str:
    return " ".join(text.split()).lower()

@task
def count_words(text: str) -> int:
    return len(text.split())

@entrypoint(checkpointer=InMemorySaver())
def analyze(inputs: dict) -> dict:
    text = normalize(inputs["text"]).result()
    count = count_words(text).result()
    return {"text": text, "words": count}

result = analyze.invoke(
    {"text": "  Functional   API  "},
    {"configurable": {"thread_id": "functional-001"}},
)
assert result == {"text": "functional api", "words": 2}
print(result)

Run and verify ​

Run to obtain normalized text and words=2. Choose Graph or Functional APIs based on how control flow is expressed and maintained, not as though they were different model providers.

Pitfalls ​

Avoid combining unrelated external writes into one task; retry and audit granularity becomes too coarse. Test compatibility with existing checkpoints after changing task or interrupt order.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. What does a task call return?

2. How can entrypoint inputs be organized?

3. Does persistence guarantee exactly-once external effects?

4. Does the Functional API replace a model provider?

Hands-on exercises ​

  1. Make normalize fail and observe propagation to the entrypoint.
  2. Create task futures for several texts and collect their results.
  3. Explain why database writes still require idempotency keys.

Official references ​

Verified on: 2026-09-17.