You raise a valid concern about the potential for inappropriate content to reach the language model (LLM) when using the Moderation API asynchronously. The OpenAI Cookbook indeed suggests using the Moderation API asynchronously to minimize latency, but it also acknowledges the potential for increased costs and the risk of inappropriate content reaching the LLM.
In the example provided (https://cookbook.openai.com/examples/how_to_use_moderation#1-input-moderation), the function execute_chat_with_moderation is designed to run the LLM's get_chat_response and the check_expression moderation function in parallel. If the moderation gets triggered (returns True), it sends back a placeholder response, otherwise, it sends back the LLM response. This is done to balance the benefits of reduced latency with the potential for increased expenses and the risk you mentioned.
However, if you are more concerned about strictly preventing any inappropriate content from reaching the LLM, you might indeed prefer to use the Moderation API synchronously before passing any information to the models. This approach is demonstrated in the Input Moderation Workflow (https://cookbook.openai.com/examples/how_to_use_moderation#workflow) where the Moderation API is used to check user input for any potentially unsafe content before it is sent to the LLM.
Here's the relevant code snippet:
async def execute_chat_with_input_moderation(user_request):
moderation_task = asyncio.create_task(check_moderation_flag(user_request))
chat_task = asyncio.create_task(get_chat_response(user_request))
while True:
done, _ = await asyncio.wait(
[moderation_task, chat_task], return_when=asyncio.FIRST_COMPLETED
)