-
Notifications
You must be signed in to change notification settings - Fork 193
🐛 Fix Anthropic json schema validation error #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 Fix Anthropic json schema validation error #457
Conversation
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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed it
import kotlin.test.assertTrue | ||
|
||
/** | ||
* Integration test for verifying the fix for the Anthropic API JSON schema validation error |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
/** | ||
* Address type enum. | ||
*/ |
There was a problem hiding this comment.
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.
/** | ||
* An address with multiple fields. | ||
*/ |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
* 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. |
There was a problem hiding this comment.
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
Co-authored-by: Bruno Lannoo <[email protected]>
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 inAnthropicLLMClient.kt
to correctly generate compliant schemas by:required
field for object schemas when applicableadditionalProperties: false
for stricter validationI 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
Checklist for all pull requests
develop
as the base branchAdditional steps for pull requests adding a new feature