fix: use async LLM calls in async task output conversion#5252
Open
shivam2407 wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix: use async LLM calls in async task output conversion#5252shivam2407 wants to merge 1 commit intocrewAIInc:mainfrom
shivam2407 wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
) When tasks run via akickoff(), _export_output() called synchronous llm.call() inside async methods (_aexecute_core, _ainvoke_guardrail_function), blocking the event loop and degrading async throughput. Add async variants throughout the output conversion chain: - OutputConverter: ato_pydantic/ato_json with asyncio.to_thread defaults - InternalInstructor: async instructor client + ato_pydantic/ato_json - Converter: ato_pydantic/ato_json using llm.acall() - Module-level: aconvert_to_model, ahandle_partial_json, aconvert_with_instructions - Task: _aexport_output() replacing 3 sync call sites in async methods Sync paths are completely untouched for backward compatibility.
Author
|
Hi @lorenzejay @greysonlalonde — this PR fixes #5230 (sync LLM calls blocking the event loop in async workflows). Would appreciate a review when you get a chance. Happy to address any feedback! |
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.
Summary
Fixes #5230
When tasks execute via
akickoff(),_export_output()calls synchronousllm.call()inside async methods (_aexecute_core,_ainvoke_guardrail_function), blocking the event loop and severely degrading async throughput.This PR adds async variants throughout the output conversion chain so that async task execution never blocks the event loop with synchronous LLM calls. Sync paths are completely untouched.
Changes
base_output_converter.pyato_pydantic()/ato_json()withasyncio.to_threaddefaults for backward compatinternal_instructor.py_get_async_client) +ato_pydantic()/ato_json(). Extracted_build_provider_model_string()to deduplicate sync/async client factoriesconverter.pyConverter.ato_pydantic()/ato_json()usingllm.acall(). Added module-levelaconvert_to_model(),ahandle_partial_json(),aconvert_with_instructions()task.py_aexport_output()usingaconvert_to_model(). Updated all 3 async call sitestest_converter.pyllm.call()is never invoked from async pathsDesign decisions
OutputConverterbase class: New async methods haveasyncio.to_threaddefaults so existing custom subclasses work without modification.Converteroverrides with native async.ato_pydanticfallback: Uses inline regex extraction instead of calling synchandle_partial_json(which would fall through tollm.call()).InternalInstructor: Lazy-creates async instructor client via_get_async_client(), cached after first use. Usesinstructor.from_litellm(acompletion)for litellm orinstructor.from_provider(..., async_mode="async")for other providers.Test plan
pytest -k "async or ato_")llm.call.assert_not_called()— sync path never invokedtest_ato_pydantic_validation_failure_never_calls_synccovers theValidationErrorfallback pathakickoff()+output_pydantictask in a real crew