fix(prompt-optimizer): support list content type in prompt optimizer#900
Merged
TimeBomb2018 merged 1 commit intorelease/v0.3.0from Apr 15, 2026
Merged
fix(prompt-optimizer): support list content type in prompt optimizer#900TimeBomb2018 merged 1 commit intorelease/v0.3.0from
TimeBomb2018 merged 1 commit intorelease/v0.3.0from
Conversation
Contributor
评审者指南(在小型 PR 上折叠)评审者指南更新提示优化器服务,以正确处理可能为字符串或文本片段列表的块内容;当遇到不支持的内容类型时,快速失败并记录日志。 提示优化器中内容类型处理的流程图flowchart TD
A["Start processing chunk"] --> B["Get content from chunk or use chunk directly"]
B --> C{Content is empty?}
C -->|Yes| D["Skip this chunk and continue"]
C -->|No| E{Content type}
E -->|String| F["Append string content to buffer"]
E -->|List| G["Iterate over list items and append each item text to buffer"]
E -->|Other| H["Log error for unsupported content type"]
H --> I["Raise exception Unsupported content type"]
F --> J["Update cache from buffer"]
G --> J
J --> K["Continue with prompt optimization"]
文件级变更
技巧与命令与 Sourcery 交互
自定义你的体验访问你的控制面板以:
获取帮助Original review guide in EnglishReviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates the prompt optimizer service to correctly handle chunk contents that may be either strings or lists of text segments, and to fail fast with logging if an unsupported content type is encountered. Flow diagram for content type handling in prompt optimizerflowchart TD
A["Start processing chunk"] --> B["Get content from chunk or use chunk directly"]
B --> C{Content is empty?}
C -->|Yes| D["Skip this chunk and continue"]
C -->|No| E{Content type}
E -->|String| F["Append string content to buffer"]
E -->|List| G["Iterate over list items and append each item text to buffer"]
E -->|Other| H["Log error for unsupported content type"]
H --> I["Raise exception Unsupported content type"]
F --> J["Update cache from buffer"]
G --> J
J --> K["Continue with prompt optimization"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - 我发现了两个问题,并给出了一些整体反馈:
- 新的列表处理分支假设每个元素都有一个
"text"键;建议使用更安全的访问方式(例如item.get("text")),并对缺失或格式不正确的条目进行处理,以避免出现意料之外的KeyError。 - 在循环中使用
_作为变量名但实际又使用了它的值,会让人困惑,因为_按惯例通常用于“未使用的变量”;建议将其重命名为更有意义的名称,例如item,以提升可读性。 - 将通用的
Exception("Unsupported content type")与记录整个content结合使用,可能会暴露体积很大或敏感的负载;建议仅记录类型或截断后的内容表示,并抛出更具体的异常类型。
给 AI Agent 的提示
Please address the comments from this code review:
## Overall Comments
- The new list-handling branch assumes every element has a 'text' key; consider using a safer access pattern (e.g., `item.get("text")`) and handling missing or malformed entries to avoid unexpected `KeyError`s.
- Using `_` as the loop variable while actually using its value is confusing, since `_` is conventionally reserved for unused variables; rename it to something meaningful like `item` for clarity.
- The generic `Exception("Unsupported content type")` combined with logging the full `content` may expose large or sensitive payloads; consider logging only the type or a truncated representation and raising a more specific exception type.
## Individual Comments
### Comment 1
<location path="api/app/services/prompt_optimizer_service.py" line_range="232-234" />
<code_context>
- buffer += content
+ if isinstance(content, str):
+ buffer += content
+ elif isinstance(content, list):
+ for _ in content:
+ buffer += _["text"]
+ else:
+ logger.error(f"Unsupported content type - {content}")
</code_context>
<issue_to_address>
**issue (bug_risk):** List branch assumes specific item structure without validation or fallback.
In this branch, each `content` element is assumed to be a dict with a `"text"` key. If an element isn’t a dict or lacks that key, you’ll get a `TypeError`/`KeyError` before reaching the explicit error handling below. Consider validating each item (e.g., `isinstance(item, dict)` and `'text' in item`) and either skipping invalid entries or raising a clearer error that includes the offending element.
</issue_to_address>
### Comment 2
<location path="api/app/services/prompt_optimizer_service.py" line_range="235-237" />
<code_context>
+ for _ in content:
+ buffer += _["text"]
+ else:
+ logger.error(f"Unsupported content type - {content}")
+ raise Exception("Unsupported content type")
cache = buffer[:-20]
</code_context>
<issue_to_address>
**🚨 suggestion (security):** Raising a bare Exception and logging the full content can be improved.
Two points:
1) Raising a generic `Exception` makes upstream handling and debugging harder; use a more specific or custom exception type.
2) Avoid logging the full `content` value, as it may be large or contain sensitive data—log only a truncated preview, type, or relevant metadata instead.
```suggestion
else:
# Log limited information to avoid dumping large or sensitive payloads
content_preview = str(content)
max_preview_length = 200
if len(content_preview) > max_preview_length:
content_preview = content_preview[:max_preview_length] + "...[truncated]"
logger.error(
"Unsupported content type: %s; preview: %s",
type(content),
content_preview,
)
raise TypeError("Unsupported content type for 'content' in prompt optimizer")
```
</issue_to_address>帮我变得更有用!请对每条评论点 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English
Hey - I've found 2 issues, and left some high level feedback:
- The new list-handling branch assumes every element has a 'text' key; consider using a safer access pattern (e.g.,
item.get("text")) and handling missing or malformed entries to avoid unexpectedKeyErrors. - Using
_as the loop variable while actually using its value is confusing, since_is conventionally reserved for unused variables; rename it to something meaningful likeitemfor clarity. - The generic
Exception("Unsupported content type")combined with logging the fullcontentmay expose large or sensitive payloads; consider logging only the type or a truncated representation and raising a more specific exception type.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new list-handling branch assumes every element has a 'text' key; consider using a safer access pattern (e.g., `item.get("text")`) and handling missing or malformed entries to avoid unexpected `KeyError`s.
- Using `_` as the loop variable while actually using its value is confusing, since `_` is conventionally reserved for unused variables; rename it to something meaningful like `item` for clarity.
- The generic `Exception("Unsupported content type")` combined with logging the full `content` may expose large or sensitive payloads; consider logging only the type or a truncated representation and raising a more specific exception type.
## Individual Comments
### Comment 1
<location path="api/app/services/prompt_optimizer_service.py" line_range="232-234" />
<code_context>
- buffer += content
+ if isinstance(content, str):
+ buffer += content
+ elif isinstance(content, list):
+ for _ in content:
+ buffer += _["text"]
+ else:
+ logger.error(f"Unsupported content type - {content}")
</code_context>
<issue_to_address>
**issue (bug_risk):** List branch assumes specific item structure without validation or fallback.
In this branch, each `content` element is assumed to be a dict with a `"text"` key. If an element isn’t a dict or lacks that key, you’ll get a `TypeError`/`KeyError` before reaching the explicit error handling below. Consider validating each item (e.g., `isinstance(item, dict)` and `'text' in item`) and either skipping invalid entries or raising a clearer error that includes the offending element.
</issue_to_address>
### Comment 2
<location path="api/app/services/prompt_optimizer_service.py" line_range="235-237" />
<code_context>
+ for _ in content:
+ buffer += _["text"]
+ else:
+ logger.error(f"Unsupported content type - {content}")
+ raise Exception("Unsupported content type")
cache = buffer[:-20]
</code_context>
<issue_to_address>
**🚨 suggestion (security):** Raising a bare Exception and logging the full content can be improved.
Two points:
1) Raising a generic `Exception` makes upstream handling and debugging harder; use a more specific or custom exception type.
2) Avoid logging the full `content` value, as it may be large or contain sensitive data—log only a truncated preview, type, or relevant metadata instead.
```suggestion
else:
# Log limited information to avoid dumping large or sensitive payloads
content_preview = str(content)
max_preview_length = 200
if len(content_preview) > max_preview_length:
content_preview = content_preview[:max_preview_length] + "...[truncated]"
logger.error(
"Unsupported content type: %s; preview: %s",
type(content),
content_preview,
)
raise TypeError("Unsupported content type for 'content' in prompt optimizer")
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
TimeBomb2018
approved these changes
Apr 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
在聚合分块内容时,扩展提示优化器对更多内容类型的处理能力。
Bug 修复:
Original summary in English
Summary by Sourcery
Handle additional content types in the prompt optimizer when aggregating chunk content.
Bug Fixes: