Extract just the query text from previous queries. Handles both simple string lists and complex nested structures.
(self, raw_prev_queries)
| 242 | return cls(query_params, http_handler) |
| 243 | |
| 244 | def _extract_query_texts(self, raw_prev_queries): |
| 245 | """ |
| 246 | Extract just the query text from previous queries. |
| 247 | Handles both simple string lists and complex nested structures. |
| 248 | """ |
| 249 | if not raw_prev_queries: |
| 250 | return [] |
| 251 | |
| 252 | query_texts = [] |
| 253 | |
| 254 | for item in raw_prev_queries: |
| 255 | if isinstance(item, str): |
| 256 | # Try to parse as JSON if it looks like JSON |
| 257 | if item.strip().startswith('[') or item.strip().startswith('{'): |
| 258 | try: |
| 259 | import json |
| 260 | parsed = json.loads(item) |
| 261 | # Recursively extract from parsed JSON |
| 262 | extracted = self._extract_from_parsed(parsed) |
| 263 | query_texts.extend(extracted) |
| 264 | except json.JSONDecodeError: |
| 265 | # If not JSON, just add the string |
| 266 | query_texts.append(item) |
| 267 | else: |
| 268 | query_texts.append(item) |
| 269 | elif isinstance(item, dict): |
| 270 | # Extract query from dict structure |
| 271 | if 'query' in item: |
| 272 | if isinstance(item['query'], dict) and 'query' in item['query']: |
| 273 | query_texts.append(item['query']['query']) |
| 274 | elif isinstance(item['query'], str): |
| 275 | query_texts.append(item['query']) |
| 276 | elif isinstance(item, list): |
| 277 | # Recursively extract from list |
| 278 | extracted = self._extract_from_parsed(item) |
| 279 | query_texts.extend(extracted) |
| 280 | |
| 281 | return query_texts |
| 282 | |
| 283 | def _extract_from_parsed(self, parsed): |
| 284 | """Helper to extract query texts from parsed JSON structures.""" |
no test coverage detected