Skip to content

refactor: use direct struct pointer for Anthropic tool InputSchema#1927

Open
davidrudduck wants to merge 1 commit intomaximhq:mainfrom
davidrudduck:fix-anthropic-tool-params
Open

refactor: use direct struct pointer for Anthropic tool InputSchema#1927
davidrudduck wants to merge 1 commit intomaximhq:mainfrom
davidrudduck:fix-anthropic-tool-params

Conversation

@davidrudduck
Copy link
Contributor

Summary

Replaces a 30-line field-by-field copy of ToolFunctionParameters with direct pointer assignment when building Anthropic tool definitions. Both the source (tool.Function.Parameters) and destination (anthropicTool.InputSchema) use the same *schemas.ToolFunctionParameters type, making the manual copy unnecessary.

This also prevents future bugs where new fields added to ToolFunctionParameters would be silently dropped by the Anthropic provider.

Changes

  • Replace field-by-field struct initialization with anthropicTool.InputSchema = tool.Function.Parameters in core/providers/anthropic/chat.go

Type of change

  • Bug fix
  • Feature
  • Refactor
  • Documentation
  • Chore/CI

Affected areas

  • Core (Go)
  • Transports (HTTP)
  • Providers/Integrations
  • Plugins
  • UI (Next.js)
  • Docs

How to test

cd core && go build ./providers/anthropic/...
make test-core PROVIDER=anthropic

Existing Anthropic provider tests validate tool parameter handling.

Breaking changes

  • Yes
  • No

Security considerations

No security impact — same data, simplified assignment.

Checklist

  • I read docs/contributing/README.md and followed the guidelines
  • I added/updated tests where appropriate
  • I verified builds succeed (Go and UI)

Replace 30-line field-by-field copy of ToolFunctionParameters with
direct pointer assignment. Both sides use the same struct type, so
the copy is unnecessary and will silently drop any new fields added
to ToolFunctionParameters in the future.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 5, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2a4cad98-6d98-4fcf-b4a7-015602959258

📥 Commits

Reviewing files that changed from the base of the PR and between 9353a23 and cf897b1.

📒 Files selected for processing (1)
  • core/providers/anthropic/chat.go

📝 Walkthrough

Summary by CodeRabbit

Release Notes

No user-visible changes in this release. This update includes internal code optimizations and maintenance improvements to improve code maintainability.

Walkthrough

This change refactors the Anthropic chat provider by simplifying InputSchema assignment for tool function parameters. The verbose field-by-field construction is replaced with a direct assignment from the existing tool.Function.Parameters structure, reducing code redundancy while maintaining identical behavior.

Changes

Cohort / File(s) Summary
InputSchema Simplification
core/providers/anthropic/chat.go
Replaced manual InputSchema construction with direct assignment from tool.Function.Parameters, eliminating 31 lines of boilerplate code while preserving all functionality.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 The code was verbose, oh so long,
Field by field, it sang a song,
But rabbit saw a simpler way,
Direct assignment saves the day!
Fewer lines, same joy—hopping along! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main refactoring change: replacing a manual struct construction with direct pointer assignment for Anthropic tool InputSchema.
Description check ✅ Passed The description covers all key sections of the template: clear summary, specific changes made, appropriate type selection (Refactor), affected areas marked, testing instructions provided, breaking changes confirmed as none, and most checklist items completed.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.


// Convert function parameters to input_schema
if tool.Function.Parameters != nil && (tool.Function.Parameters.Type != "" || tool.Function.Parameters.Properties != nil) {
anthropicTool.InputSchema = &schemas.ToolFunctionParameters{
Copy link
Contributor

Choose a reason for hiding this comment

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

@Pratham-Mishra04 I think this was to avoid altering original request right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point — the pointer is only read (serialized to JSON) after assignment so it's safe today, but a field-by-field copy is more defensive against future mutations. Happy to revert this if you'd prefer to keep the defensive copy. The main concern was that new fields added to ToolFunctionParameters would silently be dropped here without a corresponding update, but I understand the tradeoff.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes I think we directly serialize it after this so shouldnt be an issue but i'll confirm once

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you'd prefer to keep mutation safety, we could do a shallow struct copy instead:

params := *tool.Function.Parameters
anthropicTool.InputSchema = &params

This gives you the defensive copy (no shared pointer) while automatically forwarding any new fields added to ToolFunctionParameters — so no more silent field drops when the schema struct grows. Best of both worlds?

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