binary-husky/gpt_academic · error · Exception

无法提取query_type标签内容

Error message

无法提取query_type标签内容

What it means

The first LLM response was received but _extract_tag() could not find a <query_type>...</query_type> tag in it. The analyzer asks the model to emit structured XML-ish tags; if the model omits the tag, wraps it in markdown fences, or outputs a preamble before the tag, extraction fails and the exception is raised. This is a prompt-format compliance failure, not a network error.

Source

Thrown at crazy_functions/paper_fns/auto_git/query_analyzer.py:233

                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)

            # 提取子主题
            sub_topics = []
            sub_topics_text = self._extract_tag(extracted_responses[self.BASIC_QUERY_INDEX], "sub_topics")
            if sub_topics_text:
                sub_topics = [topic.strip() for topic in sub_topics_text.split(",")]

            # 提取语言
            language = self._extract_tag(extracted_responses[self.BASIC_QUERY_INDEX], "language")

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. The debug print just above shows the raw response — read it to see exactly why the tag is missing
  2. Tighten the prompt to demand output like '<query_type>repo</query_type>...' with no extra text
  3. Make _extract_tag tolerant: strip markdown fences, use re.IGNORECASE, allow surrounding whitespace
  4. As a fallback, skip the tag and let _normalize_query_type infer the type from keyword matching (the code path already exists below)

Example fix

// before
query_type = self._extract_tag(extracted_responses[self.BASIC_QUERY_INDEX], "query_type")
if not query_type:
    raise Exception("无法提取query_type标签内容")

// after
query_type = self._extract_tag(extracted_responses[self.BASIC_QUERY_INDEX], "query_type")
if not query_type:
    query_type = self._normalize_query_type("", query)  # keyword-based inference fallback
if not query_type:
    raise Exception("无法提取query_type标签内容")
Defensive patterns

Strategy: fallback

Validate before calling

import re
TAG = re.compile(r'<query_type>\s*(.*?)\s*</query_type>', re.IGNORECASE | re.DOTALL)
def has_query_type_tag(response: str) -> bool:
    return bool(TAG.search(response))

Try / catch

try:
    criteria = analyzer.analyze(query)
except Exception as e:
    if "query_type" in str(e):
        criteria = analyzer.analyze_with_defaults(query)  # keyword-based fallback path

Prevention

When it happens

Trigger: Model returns malformed output (no <query_type> tag), wraps tags in code fences, answers in a different language/format, or truncates output before the tag; regex in _extract_tag is case-sensitive or whitespace-sensitive and the model emits <Query_Type>.

Common situations: Switching to a smaller/less instruction-following model; temperature too high producing chatty preambles; max_tokens set too low so output truncates before the tag; prompt template edited without testing tag format.

Related errors


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