Skip to content

fix(prompt-optimizer): support list content type in prompt optimizer#900

Merged
TimeBomb2018 merged 1 commit intorelease/v0.3.0from
fix/prompt_optim
Apr 15, 2026
Merged

fix(prompt-optimizer): support list content type in prompt optimizer#900
TimeBomb2018 merged 1 commit intorelease/v0.3.0from
fix/prompt_optim

Conversation

@myhMARS
Copy link
Copy Markdown
Collaborator

@myhMARS myhMARS commented Apr 15, 2026

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:

  • Support list-based chunk content in the prompt optimizer when constructing the buffer.
  • Fail fast with logging when an unsupported chunk content type is encountered.

@myhMARS myhMARS requested a review from keeees April 15, 2026 03:10
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Apr 15, 2026

评审者指南(在小型 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"]
Loading

文件级变更

变更 详情 文件
强化提示优化中对块的处理,以支持基于列表的内容并拒绝不支持的类型。
  • 保护拼接逻辑,仅在内容为字符串时直接追加。
  • 通过遍历列表条目并将其 "text" 字段追加到缓冲区来支持列表内容。
  • 当内容既不是字符串也不是列表时,记录日志并抛出异常,以更早暴露意外类型。
api/app/services/prompt_optimizer_service.py

技巧与命令

与 Sourcery 交互

  • 触发新评审: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的评审评论。
  • 从评审评论生成 GitHub issue: 在某条评审评论下回复,要求 Sourcery 从该评论创建一个 issue。你也可以回复该评论 @sourcery-ai issue 来从中创建一个 issue。
  • 生成 pull request 标题: 在 pull request 标题的任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 pull request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文任意位置写上 @sourcery-ai summary,即可在你想要的位置生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 来随时(重新)生成摘要。
  • 生成评审者指南: 在 pull request 中评论 @sourcery-ai guide,即可随时(重新)生成评审者指南。
  • 解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,以解决所有 Sourcery 评论。如果你已经处理完所有评论且不想再看到它们,这会很有用。
  • 忽略所有 Sourcery 评审: 在 pull request 中评论 @sourcery-ai dismiss,以忽略所有已有的 Sourcery 评审。特别适用于你想从一次全新的评审开始——别忘了再评论 @sourcery-ai review 以触发新评审!

自定义你的体验

访问你的控制面板以:

  • 启用或禁用评审功能,例如 Sourcery 生成的 pull request 摘要、评审者指南等。
  • 更改评审语言。
  • 添加、移除或编辑自定义评审说明。
  • 调整其他评审设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates 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 optimizer

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"]
Loading

File-Level Changes

Change Details Files
Harden prompt optimization chunk handling to support list-based content and reject unsupported types.
  • Guard concatenation logic to only append directly when content is a string.
  • Add support for list content by iterating over the items and appending their "text" field to the buffer.
  • Add logging and raise an exception when content is neither a string nor a list to surface unexpected types earlier.
api/app/services/prompt_optimizer_service.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请对每条评论点 👍 或 👎,我会根据你的反馈改进后续评审。
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 unexpected KeyErrors.
  • 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.
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread api/app/services/prompt_optimizer_service.py
Comment thread api/app/services/prompt_optimizer_service.py
@TimeBomb2018 TimeBomb2018 merged commit e3c643b into release/v0.3.0 Apr 15, 2026
1 check passed
@myhMARS myhMARS deleted the fix/prompt_optim branch April 15, 2026 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants