Skip to content

fix: use async ensure_opg_approval in llm_chat example#270

Open
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
verseon0980:fix/llm-chat-async-usage
Open

fix: use async ensure_opg_approval in llm_chat example#270
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
verseon0980:fix/llm-chat-async-usage

Conversation

@verseon0980
Copy link
Copy Markdown

The example file llm_chat.py demonstrates usage of ensure_opg_approval() inside an async function without using await.

async def main():
llm = og.LLM(private_key=...)
llm.ensure_opg_approval(min_allowance=0.1) # synchronous call
result = await llm.chat(...)

This is problematic because:

ensure_opg_approval() is a synchronous, blocking function
It internally performs polling using time.sleep()
When called inside async def, it blocks the entire asyncio event loop

Impact

Any developer copying this official example will unknowingly introduce:

Frozen event loop execution
Blocked async tasks and background operations
Delayed or broken network I/O
Inability to handle timeouts or cancellations properly

This leads to incorrect async usage patterns being propagated via official documentation.

Root Cause

The example uses a synchronous function inside an async context:

llm.ensure_opg_approval(min_allowance=0.1)

Since this function blocks (via time.sleep() internally), it prevents the event loop from scheduling other coroutines.

Fix

Update the example to use the newly introduced async-safe method:

async def main():
llm = og.LLM(private_key=os.environ.get("OG_PRIVATE_KEY"))
await llm.ensure_opg_approval_async(min_allowance=0.1)
result = await llm.chat(...)

Why this works
ensure_opg_approval_async() uses asyncio.sleep()
This allows the event loop to continue running other tasks
Ensures the example is correctly aligned with asyncio best practices

Signed-off-by: verseon0980 <klokrc74@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant