binary-husky/gpt_academic · error · Exception

无法提取query_type标签内容

Error message

无法提取query_type标签内容

What it means

After collecting responses, QueryAnalyzer extracts the <query_type>...</query_type> tag from the basic-info prompt's response via _extract_tag; an empty match raises Exception('无法提取query_type标签内容'). The debug print right before shows the raw response, so the root cause is the model not following the tag format — truncated, refusals, markdown-fenced output, or a different language.

Source

Thrown at crazy_functions/review_fns/query_analyzer.py:197

                if (i * 2 + 1) < len(responses):
                    response = responses[i * 2 + 1]
                    if response is None:
                        raise Exception(f"Response {i} is None")
                    if not isinstance(response, str):
                        try:
                            response = str(response)
                        except:
                            raise Exception(f"Cannot convert response {i} to string")
                    extracted_responses.append(response)
                else:
                    raise Exception(f"未收到第 {i + 1} 个响应")

            # 解析基本信息
            query_type = self._extract_tag(extracted_responses[self.BASIC_QUERY_INDEX], "query_type")
            if not query_type:
                print(
                    f"Debug - Failed to extract query_type. Response was: {extracted_responses[self.BASIC_QUERY_INDEX]}")
                raise Exception("无法提取query_type标签内容")
            query_type = query_type.lower()

            main_topic = self._extract_tag(extracted_responses[self.BASIC_QUERY_INDEX], "main_topic")
            if not main_topic:
                print(f"Debug - Failed to extract main_topic. Using query as fallback.")
                main_topic = query

            query_type = self._normalize_query_type(query_type, query)

            # 解析arXiv参数
            try:
                arxiv_params = {
                    "query": self._extract_tag(extracted_responses[self.ARXIV_QUERY_INDEX], "query"),
                    "categories": [cat.strip() for cat in
                                   self._extract_tag(extracted_responses[self.ARXIV_CATEGORIES_INDEX],
                                                     "categories").split(",")],
                    "sort_by": self._extract_tag(extracted_responses[self.ARXIV_SORT_INDEX], "sort_by"),
                    "sort_order": self._extract_tag(extracted_responses[self.ARXIV_SORT_INDEX], "sort_order"),

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Read the printed Debug line to see what the model actually returned and adapt the prompt ('MUST wrap the answer in <query_type>...</query_type> tags, no markdown fence').
  2. Increase max_token for the basic prompt so the response is not truncated before the tag.
  3. Make _extract_tag tolerant: strip code fences and unescape HTML entities (&lt; &gt;) before regex matching, and fall back to keyword-based classification (the class already has _normalize_query_type keyword fallback).
  4. Use a stronger/instruction-following model for the analyzer step.

Example fix

# before
m = re.search(f'<{tag}>(.*?)</{tag}>', text, re.DOTALL)

# after (tolerate fenced/escaped tags)
text = text.replace('&lt;', '<').replace('&gt;', '>')
text = re.sub(r'^```.*?$', '', text, flags=re.MULTILINE)
m = re.search(f'<{tag}>(.*?)</{tag}>', text, re.DOTALL)
Defensive patterns

Strategy: fallback

Try / catch

try:
    result = analyzer.analyze(query)
except Exception as e:
    if 'query_type' in str(e):
        # model ignored the tag format: fall back to keyword heuristics
        qtype = analyzer._normalize_query_type('', query)
        result = default_analyzed_query(query, qtype)
    else:
        raise

Prevention

When it happens

Trigger: The LLM answers the BASIC prompt without literal <query_type> tags (writes 'Query type: review' instead), the response is truncated by max-token limits before the tag, the model wraps tags in code fences with escaped angle brackets (&lt;query_type&gt;), or the response is an error/refusal message.

Common situations: Weaker/local models ignoring prompt format instructions; max_tokens too small so the tag never appears; temperature/regenerate differences; prompt template drift after model upgrade; HTML-escaped tags when the response passes through markdown rendering.

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/d58f597cc87d30e5. Report an issue: GitHub.