TypeError
TypeError: object NoneType can't be used in 'await' expression
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
result = await observed_async_function()
File "/usr/local/lib/python3.10/site-packages/langfuse/decorators.py", line 58, in wrapper
response = await func(*args, **kwargs) # TypeError: object NoneType can't be used in 'await' expression
TypeError: object NoneType can't be used in 'await' expression Why it happens
The Langfuse observe decorator is designed for synchronous functions by default. When applied to async functions without using the async-compatible decorator variant, it returns None instead of a coroutine, causing a TypeError when awaited.
Detection
Monitor for TypeError exceptions mentioning 'NoneType' in await expressions in logs when using Langfuse observe on async functions.
Causes & fixes
Using Langfuse's synchronous observe decorator on an async function
Use the async-compatible observe decorator variant or ensure the decorator supports async functions by awaiting the wrapped coroutine properly.
Decorator wrapper function missing async keyword for async functions
Define the decorator wrapper as async def and await the inner async function call.
Forgetting to return the awaited result inside the async decorator wrapper
Add a return statement before awaiting the wrapped async function to propagate the result.
Code: broken vs fixed
from langfuse import observe
@observe # This decorator is synchronous
async def fetch_data():
return await some_async_call()
result = await fetch_data() # TypeError occurs here import os
from langfuse import observe_async
os.environ['LANGFUSE_API_KEY'] = os.environ.get('OPENAI_API_KEY', 'your_api_key_here') # Use env var for API key
@observe_async # Use async-compatible decorator
async def fetch_data():
return await some_async_call()
import asyncio
result = asyncio.run(fetch_data()) # Works without TypeError
print(result) Workaround
Wrap the async function call in a try/except block catching TypeError, then call the original async function without the decorator as a fallback.
Prevention
Always use decorators explicitly designed for async functions when decorating async code, or implement decorators with async support to avoid await-related TypeErrors.