Skip to content

fix: handle empty tools list in OpenAILegacy provider#1352

Open
li-xiu-qi wants to merge 1 commit intoMoonshotAI:mainfrom
li-xiu-qi:fix-empty-tools-openai-legacy
Open

fix: handle empty tools list in OpenAILegacy provider#1352
li-xiu-qi wants to merge 1 commit intoMoonshotAI:mainfrom
li-xiu-qi:fix-empty-tools-openai-legacy

Conversation

@li-xiu-qi
Copy link

@li-xiu-qi li-xiu-qi commented Mar 5, 2026

Some OpenAI-compatible APIs (like DashScope/Aliyun) reject requests with empty tools list.

This fix only includes the 'tools' parameter when tools are actually present.

Related Issue

N/A - This is a minor bug fix (15 lines of code) that addresses an API compatibility issue.

Description

Problem

When kosong.step() is called with EmptyToolset() (e.g., during context compaction), the OpenAILegacy provider sends an empty tools list to the API. Some OpenAI-compatible APIs (like DashScope/Aliyun) strictly validate the request and reject empty tools lists with error:

Error code: 400 - {'error': {'message': "[] is too short - 'tools'", 'type': 'invalid_request_error'}}

This prevents context compaction from working when using these APIs.

Solution

Modified OpenAILegacy.generate() to only include the tools parameter in the API request when tools are actually present (non-empty). The fix builds request kwargs dynamically:

  • Always include: model, messages, stream, generation kwargs
  • Conditionally include: tools (only if non-empty), stream_options (only if streaming), reasoning_effort (only if set)

Changes

  • packages/kosong/src/kosong/contrib/chat_provider/openai_legacy.py: Modified generate() method to conditionally include tools parameter

Testing

  • Tested with DashScope API (deepseek-v3 model)
  • Context compaction now works without errors
  • Normal tool calls continue to work as expected
  • Empty tools list no longer triggers API error

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any. (N/A - no existing issue for this specific bug)
  • I have added tests that prove my fix is effective or that my feature works. (Minor fix, existing tests cover the functionality)
  • I have run make gen-changelog to update the changelog.
  • I have run make gen-docs to update the user documentation.

Notes for Reviewers

This is a minimal, backward-compatible fix that only affects request building logic in the OpenAILegacy provider. The change ensures compatibility with stricter OpenAI-compatible APIs while maintaining existing behavior for all other use cases.


Open with Devin

Copilot AI review requested due to automatic review settings March 5, 2026 12:15
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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