Skip to content

Add JSON response format support and dynamic model fetching#36

Closed
ttlequals0 wants to merge 4 commits intoRichardAtCT:mainfrom
ttlequals0:feature/json-format-and-dynamic-models
Closed

Add JSON response format support and dynamic model fetching#36
ttlequals0 wants to merge 4 commits intoRichardAtCT:mainfrom
ttlequals0:feature/json-format-and-dynamic-models

Conversation

@ttlequals0
Copy link

Summary

  • Add OpenAI-compatible response_format: {"type": "json_object"} parameter for JSON mode
  • Add ModelService for dynamic model fetching from Anthropic API with fallback to static list
  • Add claude-opus-4-5-20251101 model support
  • Add JSON extraction and enforcement with comprehensive debug logging
  • Strengthen JSON mode instructions to prevent markdown code block wrapping

Changes

JSON Response Format

  • New response_format parameter in chat completions endpoint
  • JSON extraction from markdown code blocks when model does not comply
  • Fallback enforcement with strict=True mode returning [] on failure
  • Debug logging for raw response boundaries and extracted JSON structure

Dynamic Model Fetching

  • New ModelService class fetches available models from Anthropic API
  • Graceful fallback to static model list if API unavailable
  • Models refreshed on server startup

Improved JSON Instructions

  • Explicit first/last character requirements in system prompt
  • Direct prohibition of markdown code fences
  • Reinforcement via user prompt suffix

Test plan

  • All 468 unit tests pass
  • Manual test with response_format: {"type": "json_object"} request
  • Verify logs show Raw response: starts=... without markdown
  • Verify JSON array with N items or JSON object fields in debug output

Generated with Claude Code

- Add response_format parameter for OpenAI-compatible JSON mode
- Add ModelService for dynamic model fetching from Anthropic API
- Add claude-opus-4-5-20251101 model to supported models
- Add JSON extraction and enforcement methods to MessageAdapter
- Update docker-compose.yml to use published image
- Bump version to 2.3.0
Claude Code SDK was ignoring JSON_MODE_INSTRUCTION in the system prompt
and returning conversational text instead of JSON. Added JSON_PROMPT_SUFFIX
constant that is now appended to the user prompt alongside the system
prompt instruction, ensuring the model follows JSON output requirements.

Changes:
- Add JSON_PROMPT_SUFFIX constant to message_adapter.py
- Append suffix to user prompt in both streaming and non-streaming paths
- Update log messages to reflect dual-prompt approach
- Bump version to 2.3.1
- Updated JSON_MODE_INSTRUCTION with explicit first/last character rules
- Added explicit prohibition of markdown code blocks in instructions
- Updated JSON_PROMPT_SUFFIX with more concise output format
- Added log_json_structure() helper for debugging JSON responses
- Added boundary and structure logging in streaming/non-streaming paths
@ttlequals0 ttlequals0 closed this Feb 1, 2026
@ttlequals0 ttlequals0 deleted the feature/json-format-and-dynamic-models branch February 1, 2026 01:52
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.

1 participant