-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: add structured output support via json_schema
#683
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e600a51
feat: add structured output support
bogini e935838
fix: address PR #683 review feedback
bogini dcee434
fix: workaround GitHub Actions composite action output limitation
bogini 8cd2cc1
refactor: remove individual field outputs, keep only structured_outpu…
bogini ec3a934
docs: update structured output documentation for JSON-only approach
bogini f551cdf
fix: remove double error reporting in parseAndSetStructuredOutputs
bogini bf8f85c
docs: fix incorrect field naming restrictions in base-action/action.yml
bogini 9d3bab5
test: add proper test coverage for parseAndSetStructuredOutputs
bogini 84265a4
update
bogini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,349 @@ | ||
| name: Test Structured Outputs | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test-basic-types: | ||
| name: Test Basic Type Conversions | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | ||
|
|
||
| - name: Test with explicit values | ||
| id: test | ||
| uses: ./base-action | ||
| with: | ||
| prompt: | | ||
| Run this command: echo "test" | ||
|
|
||
| Then return EXACTLY these values: | ||
| - text_field: "hello" | ||
| - number_field: 42 | ||
| - boolean_true: true | ||
| - boolean_false: false | ||
| json_schema: | | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "text_field": {"type": "string"}, | ||
| "number_field": {"type": "number"}, | ||
| "boolean_true": {"type": "boolean"}, | ||
| "boolean_false": {"type": "boolean"} | ||
| }, | ||
| "required": ["text_field", "number_field", "boolean_true", "boolean_false"] | ||
| } | ||
| anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} | ||
| allowed_tools: "Bash" | ||
|
|
||
| - name: Verify outputs | ||
| run: | | ||
| # Parse the structured_output JSON | ||
| OUTPUT='${{ steps.test.outputs.structured_output }}' | ||
|
|
||
| # Test string pass-through | ||
| TEXT_FIELD=$(echo "$OUTPUT" | jq -r '.text_field') | ||
| if [ "$TEXT_FIELD" != "hello" ]; then | ||
| echo "❌ String: expected 'hello', got '$TEXT_FIELD'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Test number → string conversion | ||
| NUMBER_FIELD=$(echo "$OUTPUT" | jq -r '.number_field') | ||
| if [ "$NUMBER_FIELD" != "42" ]; then | ||
| echo "❌ Number: expected '42', got '$NUMBER_FIELD'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Test boolean → "true" conversion | ||
| BOOLEAN_TRUE=$(echo "$OUTPUT" | jq -r '.boolean_true') | ||
| if [ "$BOOLEAN_TRUE" != "true" ]; then | ||
| echo "❌ Boolean true: expected 'true', got '$BOOLEAN_TRUE'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Test boolean → "false" conversion | ||
| BOOLEAN_FALSE=$(echo "$OUTPUT" | jq -r '.boolean_false') | ||
| if [ "$BOOLEAN_FALSE" != "false" ]; then | ||
| echo "❌ Boolean false: expected 'false', got '$BOOLEAN_FALSE'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "✅ All basic type conversions correct" | ||
|
|
||
| test-complex-types: | ||
| name: Test Arrays and Objects | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | ||
|
|
||
| - name: Test complex types | ||
| id: test | ||
| uses: ./base-action | ||
| with: | ||
| prompt: | | ||
| Run: echo "ready" | ||
|
|
||
| Return EXACTLY: | ||
| - items: ["apple", "banana", "cherry"] | ||
| - config: {"key": "value", "count": 3} | ||
| - empty_array: [] | ||
| json_schema: | | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "items": { | ||
| "type": "array", | ||
| "items": {"type": "string"} | ||
| }, | ||
| "config": {"type": "object"}, | ||
| "empty_array": {"type": "array"} | ||
| }, | ||
| "required": ["items", "config", "empty_array"] | ||
| } | ||
| anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} | ||
| allowed_tools: "Bash" | ||
|
|
||
| - name: Verify JSON stringification | ||
| run: | | ||
| # Parse the structured_output JSON | ||
| OUTPUT='${{ steps.test.outputs.structured_output }}' | ||
|
|
||
| # Arrays should be JSON stringified | ||
| if ! echo "$OUTPUT" | jq -e '.items | length == 3' > /dev/null; then | ||
| echo "❌ Array not properly formatted" | ||
| echo "$OUTPUT" | jq '.items' | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Objects should be JSON stringified | ||
| if ! echo "$OUTPUT" | jq -e '.config.key == "value"' > /dev/null; then | ||
| echo "❌ Object not properly formatted" | ||
| echo "$OUTPUT" | jq '.config' | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Empty arrays should work | ||
| if ! echo "$OUTPUT" | jq -e '.empty_array | length == 0' > /dev/null; then | ||
| echo "❌ Empty array not properly formatted" | ||
| echo "$OUTPUT" | jq '.empty_array' | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "✅ All complex types handled correctly" | ||
|
|
||
| test-edge-cases: | ||
| name: Test Edge Cases | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | ||
|
|
||
| - name: Test edge cases | ||
| id: test | ||
| uses: ./base-action | ||
| with: | ||
| prompt: | | ||
| Run: echo "test" | ||
|
|
||
| Return EXACTLY: | ||
| - zero: 0 | ||
| - empty_string: "" | ||
| - negative: -5 | ||
| - decimal: 3.14 | ||
| json_schema: | | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "zero": {"type": "number"}, | ||
| "empty_string": {"type": "string"}, | ||
| "negative": {"type": "number"}, | ||
| "decimal": {"type": "number"} | ||
| }, | ||
| "required": ["zero", "empty_string", "negative", "decimal"] | ||
| } | ||
| anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} | ||
| allowed_tools: "Bash" | ||
|
|
||
| - name: Verify edge cases | ||
| run: | | ||
| # Parse the structured_output JSON | ||
| OUTPUT='${{ steps.test.outputs.structured_output }}' | ||
|
|
||
| # Zero should be "0", not empty or falsy | ||
| ZERO=$(echo "$OUTPUT" | jq -r '.zero') | ||
| if [ "$ZERO" != "0" ]; then | ||
| echo "❌ Zero: expected '0', got '$ZERO'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Empty string should be empty (not "null" or missing) | ||
| EMPTY_STRING=$(echo "$OUTPUT" | jq -r '.empty_string') | ||
| if [ "$EMPTY_STRING" != "" ]; then | ||
| echo "❌ Empty string: expected '', got '$EMPTY_STRING'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Negative numbers should work | ||
| NEGATIVE=$(echo "$OUTPUT" | jq -r '.negative') | ||
| if [ "$NEGATIVE" != "-5" ]; then | ||
| echo "❌ Negative: expected '-5', got '$NEGATIVE'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Decimals should preserve precision | ||
| DECIMAL=$(echo "$OUTPUT" | jq -r '.decimal') | ||
| if [ "$DECIMAL" != "3.14" ]; then | ||
| echo "❌ Decimal: expected '3.14', got '$DECIMAL'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "✅ All edge cases handled correctly" | ||
|
|
||
| test-name-sanitization: | ||
| name: Test Output Name Sanitization | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | ||
|
|
||
| - name: Test special characters in field names | ||
| id: test | ||
| uses: ./base-action | ||
| with: | ||
| prompt: | | ||
| Run: echo "test" | ||
| Return EXACTLY: {test-result: "passed", item_count: 10} | ||
| json_schema: | | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "test-result": {"type": "string"}, | ||
| "item_count": {"type": "number"} | ||
| }, | ||
| "required": ["test-result", "item_count"] | ||
| } | ||
| anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} | ||
| allowed_tools: "Bash" | ||
|
|
||
| - name: Verify sanitized names work | ||
| run: | | ||
| # Parse the structured_output JSON | ||
| OUTPUT='${{ steps.test.outputs.structured_output }}' | ||
|
|
||
| # Hyphens should be preserved in the JSON | ||
| TEST_RESULT=$(echo "$OUTPUT" | jq -r '.["test-result"]') | ||
| if [ "$TEST_RESULT" != "passed" ]; then | ||
| echo "❌ Hyphenated name failed: expected 'passed', got '$TEST_RESULT'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Underscores should work | ||
| ITEM_COUNT=$(echo "$OUTPUT" | jq -r '.item_count') | ||
| if [ "$ITEM_COUNT" != "10" ]; then | ||
| echo "❌ Underscore name failed: expected '10', got '$ITEM_COUNT'" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "✅ Name sanitization works" | ||
|
|
||
| test-execution-file-structure: | ||
| name: Test Execution File Format | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | ||
|
|
||
| - name: Run with structured output | ||
| id: test | ||
| uses: ./base-action | ||
| with: | ||
| prompt: "Run: echo 'complete'. Return: {done: true}" | ||
| json_schema: | | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "done": {"type": "boolean"} | ||
| }, | ||
| "required": ["done"] | ||
| } | ||
| anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} | ||
| allowed_tools: "Bash" | ||
|
|
||
| - name: Verify execution file contains structured_output | ||
| run: | | ||
| FILE="${{ steps.test.outputs.execution_file }}" | ||
|
|
||
| # Check file exists | ||
| if [ ! -f "$FILE" ]; then | ||
| echo "❌ Execution file missing" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Check for structured_output field | ||
| if ! jq -e '.[] | select(.type == "result") | .structured_output' "$FILE" > /dev/null; then | ||
| echo "❌ No structured_output in execution file" | ||
| cat "$FILE" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Verify the actual value | ||
| DONE=$(jq -r '.[] | select(.type == "result") | .structured_output.done' "$FILE") | ||
| if [ "$DONE" != "true" ]; then | ||
| echo "❌ Wrong value in execution file" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "✅ Execution file format correct" | ||
|
|
||
| test-summary: | ||
| name: Summary | ||
| runs-on: ubuntu-latest | ||
| needs: | ||
| - test-basic-types | ||
| - test-complex-types | ||
| - test-edge-cases | ||
| - test-name-sanitization | ||
| - test-execution-file-structure | ||
| if: always() | ||
| steps: | ||
| - name: Generate Summary | ||
| run: | | ||
| echo "# Structured Output Tests (Optimized)" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "Fast, deterministic tests using explicit prompts" >> $GITHUB_STEP_SUMMARY | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "| Test | Result |" >> $GITHUB_STEP_SUMMARY | ||
| echo "|------|--------|" >> $GITHUB_STEP_SUMMARY | ||
| echo "| Basic Types | ${{ needs.test-basic-types.result == 'success' && '✅ PASS' || '❌ FAIL' }} |" >> $GITHUB_STEP_SUMMARY | ||
| echo "| Complex Types | ${{ needs.test-complex-types.result == 'success' && '✅ PASS' || '❌ FAIL' }} |" >> $GITHUB_STEP_SUMMARY | ||
| echo "| Edge Cases | ${{ needs.test-edge-cases.result == 'success' && '✅ PASS' || '❌ FAIL' }} |" >> $GITHUB_STEP_SUMMARY | ||
| echo "| Name Sanitization | ${{ needs.test-name-sanitization.result == 'success' && '✅ PASS' || '❌ FAIL' }} |" >> $GITHUB_STEP_SUMMARY | ||
| echo "| Execution File | ${{ needs.test-execution-file-structure.result == 'success' && '✅ PASS' || '❌ FAIL' }} |" >> $GITHUB_STEP_SUMMARY | ||
|
|
||
| # Check if all passed | ||
| ALL_PASSED=${{ | ||
| needs.test-basic-types.result == 'success' && | ||
| needs.test-complex-types.result == 'success' && | ||
| needs.test-edge-cases.result == 'success' && | ||
| needs.test-name-sanitization.result == 'success' && | ||
| needs.test-execution-file-structure.result == 'success' | ||
| }} | ||
|
|
||
| if [ "$ALL_PASSED" = "true" ]; then | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "## ✅ All Tests Passed" >> $GITHUB_STEP_SUMMARY | ||
| else | ||
| echo "" >> $GITHUB_STEP_SUMMARY | ||
| echo "## ❌ Some Tests Failed" >> $GITHUB_STEP_SUMMARY | ||
| exit 1 | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Configuration Issue: The
allowed_toolsinput is used throughout this test file but is not defined inbase-action/action.yml. This means the tests won't work as expected since GitHub Actions won't pass this value to the environment.Recommendation: Either:
allowed_toolsinput tobase-action/action.yml, ORclaude_args: "--allowed-tools Bash"instead