fix: use async ensure_opg_approval in llm_chat example#270
Open
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
Open
fix: use async ensure_opg_approval in llm_chat example#270verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
verseon0980 wants to merge 1 commit intoOpenGradient:mainfrom
Conversation
Signed-off-by: verseon0980 <klokrc74@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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