Skip to content

Conversation

BLannoo
Copy link
Contributor

@BLannoo BLannoo commented Jul 17, 2025

Fix Anthropic API JSON Schema Validation Error

This PR fixes an issue with the Anthropic API integration where complex nested tool structures were causing JSON schema validation errors. The Anthropic API requires JSON schemas to comply with JSON Schema draft 2020-12, but our current implementation was generating invalid nested structures.

The fix modifies the getTypeMapForParameter() function in AnthropicLLMClient.kt to correctly generate compliant schemas by:

  1. Eliminating invalid nesting by merging type information directly into property schemas
  2. Adding the required required field for object schemas when applicable
  3. Including schema metadata like additionalProperties: false for stricter validation

I first created an example to reproduce the issue (AnthropicSchemaErrorAgent.kt) most likely that one should be deleted before merging and an integration test (AnthropicSchemaValidationIntegrationTest.kt) that hopefully can be kept.

Most of these changes are generated in collaboration with Junie, so certainly could use a thorough review.


Type of the change

  • New feature
  • Bug fix
  • Documentation fix
  • Tests improvement

Checklist for all pull requests

  • The pull request has a description of the proposed change
  • I read the Contributing Guidelines before opening the pull request
  • The pull request uses develop as the base branch
  • Tests for the changes have been added
  • All new and existing tests passed
Additional steps for pull requests adding a new feature
  • An issue describing the proposed change exists
  • The pull request includes a link to the issue
  • The change was discussed and approved in the issue
  • Docs have been added / updated

This commit fixes an issue with the Anthropic API integration where complex nested tool structures were causing JSON schema validation errors. The Anthropic API requires JSON schemas to comply with JSON Schema draft 2020-12, but our current implementation was generating invalid nested structures.
@skarpovdev skarpovdev self-requested a review July 17, 2025 18:58
@EugeneTheDev EugeneTheDev self-requested a review July 18, 2025 08:37
Copy link
Contributor

@EugeneTheDev EugeneTheDev left a comment

Choose a reason for hiding this comment

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

Please take a look at my comment, but other than that LGTM

@@ -0,0 +1,241 @@
package ai.koog.agents.example.anthropic
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this file, it's not needed here as it was used only to reproduce the error. I see the same logic already being added as an integration test, which is appropriate place for such verifications.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes that was definitely the plan, just kept it in case it helped for the review.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed it

@BLannoo BLannoo marked this pull request as ready for review July 18, 2025 09:21
@BLannoo BLannoo merged commit bb35268 into JetBrains:develop Jul 18, 2025
4 of 5 checks passed
@BLannoo BLannoo deleted the brunolannoo/fix-anthropic-json-schema-validation branch July 18, 2025 09:21
import kotlin.test.assertTrue

/**
* Integration test for verifying the fix for the Anthropic API JSON schema validation error
Copy link
Contributor

@skarpovdev skarpovdev Jul 18, 2025

Choose a reason for hiding this comment

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

Let's remove the mention of the fix and just describe what case the test verifies

* Integration test for verifying the fix for the Anthropic API JSON schema validation error
* when using complex nested structures in tool parameters.
*
* The issue was in the AnthropicLLMClient.kt file, specifically in the getTypeMapForParameter() function
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, it just describes what wasn't working.

* The issue was in the AnthropicLLMClient.kt file, specifically in the getTypeMapForParameter() function
* that converts ToolDescriptor objects to JSON schemas for the Anthropic API.
*
* The problem was that when processing ToolParameterType.Object, the function created invalid nested structures
Copy link
Contributor

Choose a reason for hiding this comment

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

same

* "type": {"type": "string"} // Invalid nesting
* }
*
* This test verifies that the fix works by creating an agent with the Anthropic API and a tool
Copy link
Contributor

Choose a reason for hiding this comment

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

same

Comment on lines +63 to +65
/**
* Address type enum.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd remove the comment. It appears self-explanatory.

Comment on lines +71 to +73
/**
* An address with multiple fields.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

same

)

/**
* A user profile with nested structures.
Copy link
Contributor

Choose a reason for hiding this comment

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

same

/**
* A complex nested tool that demonstrates the JSON schema validation error.
* This tool has parameters with complex nested structures that would trigger
* the error in the Anthropic API before the fix.
Copy link
Contributor

Choose a reason for hiding this comment

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

another mention of the fix

Comment on lines +189 to +196
* Test that verifies the fix for the Anthropic API JSON schema validation error
* when using complex nested structures in tool parameters.
*
* Before the fix, this test would fail with an error like:
* "tools.0.custom.input_schema: JSON schema is invalid. It must match JSON Schema draft 2020-12"
*
* After the fix, the test should pass, demonstrating that the Anthropic API
* can now correctly handle complex nested structures in tool parameters.
Copy link
Contributor

Choose a reason for hiding this comment

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

another mention of the fix and the problem that was once there

slawa4s pushed a commit that referenced this pull request Jul 31, 2025
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.

3 participants