How to make asynchronous requests in plug-ins #422

Closed
opened 2026-02-22 17:28:46 -05:00 by yindo · 1 comment
Owner

Originally created by @SugarFatFree on GitHub (Dec 18, 2025).

async with code will not be executed,What am I supposed to do?

    def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        all_results = loop.run_until_complete(call_deepseek_api())
        loop.close()
async def call_deepseek_api():
    url = 'https://XXX'
    logging.info("print : test.call_deepseek_api")

    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer XXXX'
    }

    payload = {
        "model": "deepseek-chat",
        "messages": [
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Hello!"
            }
        ],
        "stream": True
    }

    async with aiohttp.ClientSession() as session:
        try:
            async with session.post(url, headers=headers, json=payload) as response:

                if response.status == 200:
                    result = await response.json()
                    logging.info("API success:")
                    logging.info(json.dumps(result, indent=2, ensure_ascii=False))
                else:
                    error_text = await response.text()


        except Exception as e:
            logging.info(f"error: {e}")

Originally created by @SugarFatFree on GitHub (Dec 18, 2025). async with code will not be executed,What am I supposed to do? ``` def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) all_results = loop.run_until_complete(call_deepseek_api()) loop.close() ``` ``` async def call_deepseek_api(): url = 'https://XXX' logging.info("print : test.call_deepseek_api") headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer XXXX' } payload = { "model": "deepseek-chat", "messages": [ { "role": "system", "content": "You are a helpful assistant." }, { "role": "user", "content": "Hello!" } ], "stream": True } async with aiohttp.ClientSession() as session: try: async with session.post(url, headers=headers, json=payload) as response: if response.status == 200: result = await response.json() logging.info("API success:") logging.info(json.dumps(result, indent=2, ensure_ascii=False)) else: error_text = await response.text() except Exception as e: logging.info(f"error: {e}") ```
yindo closed this issue 2026-02-22 17:28:46 -05:00
Author
Owner

@SugarFatFree commented on GitHub (Dec 18, 2025):

aiohttp problem, please delete this

@SugarFatFree commented on GitHub (Dec 18, 2025): aiohttp problem, please delete this
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-plugins#422