|
20 | 20 | import time |
21 | 21 | import uuid |
22 | 22 | from collections.abc import AsyncIterator |
23 | | -from typing import TYPE_CHECKING |
| 23 | +from typing import TYPE_CHECKING, Any |
24 | 24 |
|
25 | 25 | from opensift.adapters.base.registry import AdapterRegistry |
26 | 26 | from opensift.core.classifier import ResultClassifier |
@@ -444,13 +444,12 @@ async def batch_search(self, request: BatchSearchRequest) -> BatchSearchResponse |
444 | 444 | for query in request.queries |
445 | 445 | ] |
446 | 446 |
|
447 | | - responses = await asyncio.gather(*tasks, return_exceptions=True) |
| 447 | + raw_responses: list[SearchResponse | BaseException] = await asyncio.gather(*tasks, return_exceptions=True) |
448 | 448 |
|
449 | 449 | results: list[SearchResponse] = [] |
450 | | - for i, resp in enumerate(responses): |
451 | | - if isinstance(resp, Exception): |
| 450 | + for i, resp in enumerate(raw_responses): |
| 451 | + if isinstance(resp, BaseException): |
452 | 452 | logger.warning("Batch query %d failed: %s", i, resp) |
453 | | - # Create an error response stub |
454 | 453 | results.append( |
455 | 454 | SearchResponse( |
456 | 455 | request_id=f"req_batch_{i}_error", |
@@ -584,25 +583,29 @@ async def _execute_searches( |
584 | 583 | logger.warning("No search adapter available, returning empty results") |
585 | 584 | return [] |
586 | 585 |
|
587 | | - tasks: list[asyncio.Task] = [] |
| 586 | + tasks: list[asyncio.Task[Any]] = [] |
588 | 587 | task_meta: list[tuple[str, bool]] = [] # (adapter_name, use_paper_path) |
589 | 588 |
|
590 | 589 | for adapter in adapters: |
591 | 590 | use_paper_path = hasattr(adapter, "search_papers") and callable(adapter.search_papers) |
592 | 591 | for query in search_queries: |
593 | 592 | if use_paper_path: |
594 | | - tasks.append(adapter.search_papers(query, request.options)) # type: ignore[union-attr] |
| 593 | + tasks.append( |
| 594 | + asyncio.ensure_future( |
| 595 | + adapter.search_papers(query, request.options) # type: ignore[attr-defined] |
| 596 | + ) |
| 597 | + ) |
595 | 598 | else: |
596 | | - tasks.append(adapter.search_and_normalize(query, request.options)) |
| 599 | + tasks.append(asyncio.ensure_future(adapter.search_and_normalize(query, request.options))) |
597 | 600 | task_meta.append((adapter.name, use_paper_path)) |
598 | 601 |
|
599 | | - raw_results = await asyncio.gather(*tasks, return_exceptions=True) |
| 602 | + raw_results: list[Any] = await asyncio.gather(*tasks, return_exceptions=True) |
600 | 603 |
|
601 | 604 | seen_titles: set[str] = set() |
602 | 605 | items: list[ResultItem] = [] |
603 | 606 |
|
604 | 607 | for i, result in enumerate(raw_results): |
605 | | - if isinstance(result, Exception): |
| 608 | + if isinstance(result, BaseException): |
606 | 609 | adapter_name, _ = task_meta[i] |
607 | 610 | logger.warning("Search query failed on adapter '%s': %s", adapter_name, result) |
608 | 611 | continue |
|
0 commit comments