Skip to content

feat(anthropic): support XML mode#1693

Closed
m1kl0sh wants to merge 13 commits into567-labs:mainfrom
m1kl0sh:support-xml-schema-for-anthropic
Closed

feat(anthropic): support XML mode#1693
m1kl0sh wants to merge 13 commits into567-labs:mainfrom
m1kl0sh:support-xml-schema-for-anthropic

Conversation

@m1kl0sh
Copy link

@m1kl0sh m1kl0sh commented Jul 11, 2025

This PR adds support for XML mode when making Anthropic (Claude) calls. Claude performs better with XML-tagged prompts, and this update allows developers to use their existing high-performing prompts with Instructor. It eliminates the need for custom retry logic, XML parsing, and other workaround solutions.

This is one proposed approach to achieving the above. I'd like to get feedback before adding more to this PR, such as documentation.


Important

Adds XML mode support for Anthropic calls, including XML schema generation and parsing, with tests and dependency updates.

  • Behavior:
    • Adds ANTHROPIC_XML mode to from_anthropic() in client_anthropic.py.
    • Supports XML schema generation and parsing in function_calls.py with parse_anthropic_xml().
    • Updates handle_response_model() in process_response.py to handle ANTHROPIC_XML mode.
  • Schema Generation:
    • Adds xml_schema and xsd_schema class properties to OpenAISchema in function_calls.py for XML and XSD schema generation.
  • Utilities:
    • Adds extract_xml_from_codeblock() to utils.py for XML extraction from text.
  • Testing:
    • New tests in test_xml.py to validate XML mode functionality, including async support and schema generation.
  • Dependencies:
    • Adds xmlschema to pyproject.toml dependencies for XML validation.

This description was created by Ellipsis for 404bd48. You can customize this summary. It will automatically update as commits are pushed.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Caution

Changes requested ❌

Reviewed everything up to 404bd48 in 1 minute and 58 seconds. Click for details.
  • Reviewed 750 lines of code in 7 files
  • Skipped 1 files when reviewing.
  • Skipped posting 5 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. instructor/function_calls.py:212
  • Draft comment:
    The recursive XSD schema generation logic is quite complex. Consider adding more inline comments to clarify the recursion and indentation handling for future maintainability.
  • Reason this comment was not posted:
    Confidence changes required: 80% <= threshold 85% None
2. tests/llm/test_anthropic/test_xml.py:11
  • Draft comment:
    Comprehensive tests for XML mode look good; ensure these tests continue to cover edge cases (e.g. missing text blocks or malformed XML) in future updates.
  • Reason this comment was not posted:
    Confidence changes required: 50% <= threshold 85% None
3. pyproject.toml:20
  • Draft comment:
    Typographical issue: The dependency declaration for xmlschema includes a space and parentheses ("xmlschema (>=4.1.0,<5.0.0)") which is inconsistent with the other entries. It would be clearer to use the format "xmlschema>=4.1.0,<5.0.0".
  • Reason this comment was not posted:
    Marked as duplicate.
4. tests/llm/test_anthropic/test_xml.py:123
  • Draft comment:
    Typographical error: In the string 'Create a admin user for a model with a name and role.', the article should be 'an' instead of 'a'.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 0% vs. threshold = 85% This is a minor grammatical error in a test string. The test is focused on validating the XML response functionality and model behavior, not the grammatical correctness of the test input. The grammatical error doesn't affect the test's functionality or results. The comment is purely cosmetic and doesn't impact code quality or behavior. The grammatical error could be seen as unprofessional in code that ships to users. However, this is just a test string used internally. Since this is a test file and the grammatical error doesn't affect functionality or readability of the tests, fixing it would be purely cosmetic and not worth a PR comment. Delete the comment as it points out a minor grammatical issue in a test string that doesn't impact functionality or code quality.
5. tests/llm/test_anthropic/test_xml.py:95
  • Draft comment:
    Typographical issue: In the system message, the phrase 'Family members here is just asking for a list of names' should use 'are' instead of 'is' to properly agree with the plural noun 'Family members'.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 10% vs. threshold = 85% This is a purely grammatical suggestion that doesn't affect code functionality. The system message is just used for testing and the grammatical error doesn't impact the test's effectiveness or clarity. The rules specifically say not to make purely informative comments that don't require code changes. The grammatical error could potentially confuse other developers reading the code. Proper grammar in comments and strings can make code more professional. While proper grammar is good, this is a test file and the message is clear enough despite the minor grammatical error. The rules explicitly state not to make purely informative comments. Delete the comment as it's a purely informative grammatical suggestion that doesn't impact code functionality or clarity.

Workflow ID: wflow_rvap2MzFY68M1sLD

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

m1kl0sh and others added 3 commits July 11, 2025 11:12
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
@jxnl
Copy link
Collaborator

jxnl commented Jul 11, 2025

this is amazing will take a look this weekend!

@m1kl0sh
Copy link
Author

m1kl0sh commented Jul 12, 2025

this is amazing will take a look this weekend!

Thanks, @jxnl — looking forward to your feedback!

@jxnl
Copy link
Collaborator

jxnl commented Jul 18, 2025

OK, I tried some stuff locally. It looks like it needs a lot more prompting.

@jxnl jxnl closed this Sep 5, 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.

2 participants