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
- 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').
- Increase max_token for the basic prompt so the response is not truncated before the tag.
- Make _extract_tag tolerant: strip code fences and unescape HTML entities (< >) before regex matching, and fall back to keyword-based classification (the class already has _normalize_query_type keyword fallback).
- 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('<', '<').replace('>', '>')
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
- State the tag format in the prompt explicitly and forbid markdown fences around it.
- Give the basic-info prompt enough max_token budget to finish.
- Keep a keyword-based classifier as fallback for models with weak instruction following.
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 (<query_type>), 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
- 无法提取query_type标签内容
- GPT is not generating proper code.
- GPT is not generating proper code.
- Cannot convert response {i} to string
- 未收到第 {i + 1} 个响应
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/d58f597cc87d30e5.
Report an issue: GitHub.