Skip to content

feat: auto-append /v1 to embedding_api_base in OpenAI embedding provider#6863

Open
Izayoi9 wants to merge 1 commit intoAstrBotDevs:masterfrom
Izayoi9:fix/openai-embedding-auto-append-v1
Open

feat: auto-append /v1 to embedding_api_base in OpenAI embedding provider#6863
Izayoi9 wants to merge 1 commit intoAstrBotDevs:masterfrom
Izayoi9:fix/openai-embedding-auto-append-v1

Conversation

@Izayoi9
Copy link

@Izayoi9 Izayoi9 commented Mar 23, 2026

Fixes #6855

当用户配置 embedding_api_base 时未填写 /v1 后缀,OpenAI SDK 不会自动补全,导致请求路径错误(如 https://api.openai.com/embeddings 而非 https://api.openai.com/v1/embeddings)。

Modifications / 改动点

  • astrbot/core/provider/sources/openai_embedding_source.py 中,初始化 api_base 后增加判断:若 URL 不以 /v1/v1/ 结尾,则自动补全 /v1

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

修改前,用户配置 embedding_api_basehttps://api.openai.com 时,实际请求路径为 https://api.openai.com/embeddings,返回 404。

修改后,自动补全为 https://api.openai.com/v1,请求路径正确为 https://api.openai.com/v1/embeddings


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了"验证步骤"和"运行截图"

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

New Features:

  • Automatically append the /v1 suffix to embedding_api_base when it is omitted in the OpenAI embedding provider configuration.

…er (AstrBotDevs#6855)

When users configure `embedding_api_base` without the `/v1` suffix,
the OpenAI SDK does not auto-complete it, causing request path errors.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@auto-assign auto-assign bot requested review from Raven95676 and Soulter March 23, 2026 17:29
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 23, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求解决了 OpenAI SDK 在处理 embedding_api_base URL 时未能自动添加 /v1 后缀的问题,该问题会导致 404 错误。通过引入新的逻辑,确保 OpenAI 嵌入提供程序的 API 基地址始终包含 /v1 路径段,从而提高了系统的健壮性和用户体验。

Highlights

  • 自动补全 OpenAI API 基地址: 解决了当用户配置 embedding_api_base 时未填写 /v1 后缀导致 OpenAI SDK 请求路径错误的问题,现在会自动补全 /v1
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
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 - I've left some high level feedback:

  • Consider whether this /v1 auto-append logic should be shared with other OpenAI-compatible providers (chat, completions, etc.) so that API base handling is consistent across the codebase.
  • The unconditional appending of /v1 might break OpenAI-compatible endpoints that intentionally omit the version (or use a different path); you may want to gate this behavior behind a config flag or limit it to known OpenAI hosts.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider whether this `/v1` auto-append logic should be shared with other OpenAI-compatible providers (chat, completions, etc.) so that API base handling is consistent across the codebase.
- The unconditional appending of `/v1` might break OpenAI-compatible endpoints that intentionally omit the version (or use a different path); you may want to gate this behavior behind a config flag or limit it to known OpenAI hosts.

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.

@dosubot dosubot bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Mar 23, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request modifies the OpenAI embedding source to ensure the API base URL consistently ends with /v1. A review comment points out that the current string manipulation approach might incorrectly handle URLs with query parameters, suggesting the use of urllib.parse for a more robust solution.

Comment on lines +30 to +31
if api_base and not api_base.endswith("/v1") and not api_base.endswith("/v1/"):
api_base = api_base.rstrip("/") + "/v1"
Copy link
Contributor

Choose a reason for hiding this comment

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

high

当前使用字符串操作的实现方式,在处理带有查询参数的 URL 时可能会出错。例如,如果 embedding_api_base 被设置为 https://example.com/api?key=123,代码会将其错误地更改为 https://example.com/api?key=123/v1,这是一个无效的 URL。

建议使用 urllib.parse 来处理 URL,这样可以确保只修改路径部分,而不会影响 URL 的其他部分(如查询参数),使逻辑更加健壮。

        if api_base:
            from urllib.parse import urlparse, urlunparse
            parsed = urlparse(api_base)
            if not parsed.path.rstrip("/").endswith("/v1"):
                new_path = parsed.path.rstrip("/") + "/v1"
                api_base = urlunparse(parsed._replace(path=new_path))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]调用OpenAI embedding类型的embedding provider时不会自动补全/v1

1 participant