Raised exceptions in @task doesn't get caught #584

Closed
opened 2026-02-20 17:40:49 -05:00 by yindo · 5 comments
Owner

Originally created by @Ilaiwi on GitHub (Apr 16, 2025).

Originally assigned to: @nfcampos on GitHub.

Checked other resources

  • This is a bug, not a usage question. For questions, please use GitHub Discussions.
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

import asyncio
import logging

from langgraph.func import entrypoint
from langgraph.func import task


@task()
async def my_task(number: int):
    await asyncio.sleep(1)
    return number * 2


@task()
async def task_with_exception(number: int):
    await asyncio.sleep(1)
    raise Exception("This is a test exception")


@entrypoint()
async def my_workflow(number: int):
    await my_task(number)
    try:
        await task_with_exception(number)
    except Exception as e:
        logging.error(f"Error in task_with_exception: {e}")
    await my_task(number)
    return "done"


async def main():
    await my_workflow.ainvoke(1)


if __name__ == "__main__":
    asyncio.run(main())

Error Message and Stack Trace (if applicable)

ERROR:root:Error in task_with_exception: This is a test exception
Traceback (most recent call last):
  File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/src/components/shared/types/tests/main.py", line 36, in <module>
    asyncio.run(main())
  File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/src/components/shared/types/tests/main.py", line 32, in main
    await my_workflow.ainvoke(1)
  File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py", line 2773, in ainvoke
    async for chunk in self.astream(
  File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py", line 2655, in astream
    async for _ in runner.atick(
  File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/src/components/shared/types/tests/main.py", line 24, in my_workflow
    await task_with_exception(number)
  File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/pregel/retry.py", line 127, in arun_with_retry
    return await task.proc.ainvoke(task.input, config)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/utils/runnable.py", line 671, in ainvoke
    input = await asyncio.create_task(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/utils/runnable.py", line 439, in ainvoke
    ret = await self.afunc(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/src/components/shared/types/tests/main.py", line 17, in task_with_exception
    raise Exception("This is a test exception")
Exception: This is a test exception
During task with name 'task_with_exception' and id '5fb8df1b-1f89-e80e-e9a5-8874a3516972'

Description

  • run the code
  • the code should not error but it does

System Info

System Information

OS: Darwin
OS Version: Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:22 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6041
Python Version: 3.12.9 (main, Feb 4 2025, 14:38:38) [Clang 16.0.0 (clang-1600.0.26.6)]

Package Information

langchain_core: 0.3.52
langchain: 0.3.23
langsmith: 0.3.30
langchain_anthropic: 0.3.9
langchain_google_genai: 2.0.10
langchain_openai: 0.3.12
langchain_text_splitters: 0.3.8
langgraph_checkpoint_dynamodb: Installed. No version info available.
langgraph_sdk: 0.1.61

Optional packages not installed

langserve

Other Dependencies

anthropic<1,>=0.47.0: Installed. No version info available.
async-timeout<5.0.0,>=4.0.0;: Installed. No version info available.
filetype: 1.2.0
google-generativeai: 0.8.4
httpx: 0.27.2
jsonpatch<2.0,>=1.33: Installed. No version info available.
langchain-anthropic;: Installed. No version info available.
langchain-aws;: Installed. No version info available.
langchain-azure-ai;: Installed. No version info available.
langchain-cohere;: Installed. No version info available.
langchain-community;: Installed. No version info available.
langchain-core<1.0.0,>=0.3.41: Installed. No version info available.
langchain-core<1.0.0,>=0.3.49: Installed. No version info available.
langchain-core<1.0.0,>=0.3.51: Installed. No version info available.
langchain-deepseek;: Installed. No version info available.
langchain-fireworks;: Installed. No version info available.
langchain-google-genai;: Installed. No version info available.
langchain-google-vertexai;: Installed. No version info available.
langchain-groq;: Installed. No version info available.
langchain-huggingface;: Installed. No version info available.
langchain-mistralai;: Installed. No version info available.
langchain-ollama;: Installed. No version info available.
langchain-openai;: Installed. No version info available.
langchain-perplexity;: Installed. No version info available.
langchain-text-splitters<1.0.0,>=0.3.8: Installed. No version info available.
langchain-together;: Installed. No version info available.
langchain-xai;: Installed. No version info available.
langsmith-pyo3: Installed. No version info available.
langsmith<0.4,>=0.1.125: Installed. No version info available.
langsmith<0.4,>=0.1.17: Installed. No version info available.
openai-agents: Installed. No version info available.
openai<2.0.0,>=1.68.2: Installed. No version info available.
opentelemetry-api: Installed. No version info available.
opentelemetry-exporter-otlp-proto-http: Installed. No version info available.
opentelemetry-sdk: Installed. No version info available.
orjson: 3.10.16
packaging: 24.2
packaging<25,>=23.2: Installed. No version info available.
pydantic: 2.11.3
pydantic<3.0.0,>=2.5.2;: Installed. No version info available.
pydantic<3.0.0,>=2.7.4: Installed. No version info available.
pydantic<3.0.0,>=2.7.4;: Installed. No version info available.
pytest: 8.3.5
PyYAML>=5.3: Installed. No version info available.
requests: 2.32.3
requests-toolbelt: 1.0.0
requests<3,>=2: Installed. No version info available.
rich: 14.0.0
SQLAlchemy<3,>=1.4: Installed. No version info available.
tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available.
tiktoken<1,>=0.7: Installed. No version info available.
typing-extensions>=4.7: Installed. No version info available.
zstandard: 0.23.0

Originally created by @Ilaiwi on GitHub (Apr 16, 2025). Originally assigned to: @nfcampos on GitHub. ### Checked other resources - [x] This is a bug, not a usage question. For questions, please use GitHub Discussions. - [x] I added a clear and detailed title that summarizes the issue. - [x] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example). - [x] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue. ### Example Code ```python import asyncio import logging from langgraph.func import entrypoint from langgraph.func import task @task() async def my_task(number: int): await asyncio.sleep(1) return number * 2 @task() async def task_with_exception(number: int): await asyncio.sleep(1) raise Exception("This is a test exception") @entrypoint() async def my_workflow(number: int): await my_task(number) try: await task_with_exception(number) except Exception as e: logging.error(f"Error in task_with_exception: {e}") await my_task(number) return "done" async def main(): await my_workflow.ainvoke(1) if __name__ == "__main__": asyncio.run(main()) ``` ### Error Message and Stack Trace (if applicable) ```shell ERROR:root:Error in task_with_exception: This is a test exception Traceback (most recent call last): File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/src/components/shared/types/tests/main.py", line 36, in <module> asyncio.run(main()) File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 195, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/src/components/shared/types/tests/main.py", line 32, in main await my_workflow.ainvoke(1) File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py", line 2773, in ainvoke async for chunk in self.astream( File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/pregel/__init__.py", line 2655, in astream async for _ in runner.atick( File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/src/components/shared/types/tests/main.py", line 24, in my_workflow await task_with_exception(number) File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/pregel/retry.py", line 127, in arun_with_retry return await task.proc.ainvoke(task.input, config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/utils/runnable.py", line 671, in ainvoke input = await asyncio.create_task( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/.venv/lib/python3.12/site-packages/langgraph/utils/runnable.py", line 439, in ainvoke ret = await self.afunc(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/ahmadilaiwi/Documents/ilaiwi/WebApp/apps/codegen2/src/components/shared/types/tests/main.py", line 17, in task_with_exception raise Exception("This is a test exception") Exception: This is a test exception During task with name 'task_with_exception' and id '5fb8df1b-1f89-e80e-e9a5-8874a3516972' ``` ### Description - run the code - the code should not error but it does ### System Info System Information ------------------ > OS: Darwin > OS Version: Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:22 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6041 > Python Version: 3.12.9 (main, Feb 4 2025, 14:38:38) [Clang 16.0.0 (clang-1600.0.26.6)] Package Information ------------------- > langchain_core: 0.3.52 > langchain: 0.3.23 > langsmith: 0.3.30 > langchain_anthropic: 0.3.9 > langchain_google_genai: 2.0.10 > langchain_openai: 0.3.12 > langchain_text_splitters: 0.3.8 > langgraph_checkpoint_dynamodb: Installed. No version info available. > langgraph_sdk: 0.1.61 Optional packages not installed ------------------------------- > langserve Other Dependencies ------------------ > anthropic<1,>=0.47.0: Installed. No version info available. > async-timeout<5.0.0,>=4.0.0;: Installed. No version info available. > filetype: 1.2.0 > google-generativeai: 0.8.4 > httpx: 0.27.2 > jsonpatch<2.0,>=1.33: Installed. No version info available. > langchain-anthropic;: Installed. No version info available. > langchain-aws;: Installed. No version info available. > langchain-azure-ai;: Installed. No version info available. > langchain-cohere;: Installed. No version info available. > langchain-community;: Installed. No version info available. > langchain-core<1.0.0,>=0.3.41: Installed. No version info available. > langchain-core<1.0.0,>=0.3.49: Installed. No version info available. > langchain-core<1.0.0,>=0.3.51: Installed. No version info available. > langchain-deepseek;: Installed. No version info available. > langchain-fireworks;: Installed. No version info available. > langchain-google-genai;: Installed. No version info available. > langchain-google-vertexai;: Installed. No version info available. > langchain-groq;: Installed. No version info available. > langchain-huggingface;: Installed. No version info available. > langchain-mistralai;: Installed. No version info available. > langchain-ollama;: Installed. No version info available. > langchain-openai;: Installed. No version info available. > langchain-perplexity;: Installed. No version info available. > langchain-text-splitters<1.0.0,>=0.3.8: Installed. No version info available. > langchain-together;: Installed. No version info available. > langchain-xai;: Installed. No version info available. > langsmith-pyo3: Installed. No version info available. > langsmith<0.4,>=0.1.125: Installed. No version info available. > langsmith<0.4,>=0.1.17: Installed. No version info available. > openai-agents: Installed. No version info available. > openai<2.0.0,>=1.68.2: Installed. No version info available. > opentelemetry-api: Installed. No version info available. > opentelemetry-exporter-otlp-proto-http: Installed. No version info available. > opentelemetry-sdk: Installed. No version info available. > orjson: 3.10.16 > packaging: 24.2 > packaging<25,>=23.2: Installed. No version info available. > pydantic: 2.11.3 > pydantic<3.0.0,>=2.5.2;: Installed. No version info available. > pydantic<3.0.0,>=2.7.4: Installed. No version info available. > pydantic<3.0.0,>=2.7.4;: Installed. No version info available. > pytest: 8.3.5 > PyYAML>=5.3: Installed. No version info available. > requests: 2.32.3 > requests-toolbelt: 1.0.0 > requests<3,>=2: Installed. No version info available. > rich: 14.0.0 > SQLAlchemy<3,>=1.4: Installed. No version info available. > tenacity!=8.4.0,<10.0.0,>=8.1.0: Installed. No version info available. > tiktoken<1,>=0.7: Installed. No version info available. > typing-extensions>=4.7: Installed. No version info available. > zstandard: 0.23.0
yindo closed this issue 2026-02-20 17:40:49 -05:00
Author
Owner

@hinthornw commented on GitHub (Apr 16, 2025):

Good find. Can repro

@hinthornw commented on GitHub (Apr 16, 2025): Good find. Can repro
Author
Owner

@Ilaiwi commented on GitHub (Apr 17, 2025):

any insights here @hinthornw ? We are happy to contribute a solution. We found out that the error can be caught on the entrypoint but this just a workaround to what we are actually usign the functional api for

@Ilaiwi commented on GitHub (Apr 17, 2025): any insights here @hinthornw ? We are happy to contribute a solution. We found out that the error can be caught on the entrypoint but this just a workaround to what we are actually usign the functional api for
Author
Owner

@Ilaiwi commented on GitHub (Apr 17, 2025):

actually even wrapping tasks with entrypoint introduces issues with checkpointers since the error thrown from @task is not seralizable..

TypeError: Type is not msgpack serializable: Future
TypeError('Type is not msgpack serializable: Future')Traceback (most recent call last):
@Ilaiwi commented on GitHub (Apr 17, 2025): actually even wrapping tasks with entrypoint introduces issues with checkpointers since the error thrown from `@task` is not seralizable.. ``` TypeError: Type is not msgpack serializable: Future TypeError('Type is not msgpack serializable: Future')Traceback (most recent call last): ```
Author
Owner

@hinthornw commented on GitHub (Apr 21, 2025):

Yes futures/coros aren't something you can serialize (even with something like pickle, by design).

Will try to get a fix out soon

@hinthornw commented on GitHub (Apr 21, 2025): Yes futures/coros aren't something you can serialize (even with something like pickle, by design). Will try to get a fix out soon
Author
Owner

@nfcampos commented on GitHub (May 23, 2025):

Fixing in https://github.com/langchain-ai/langgraph/pull/4802

@nfcampos commented on GitHub (May 23, 2025): Fixing in https://github.com/langchain-ai/langgraph/pull/4802
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#584