Skip to content

Fix/json system prompt customization#2031

Open
Waqar53 wants to merge 4 commits into567-labs:mainfrom
Waqar53:fix/json-system-prompt-customization
Open

Fix/json system prompt customization#2031
Waqar53 wants to merge 4 commits into567-labs:mainfrom
Waqar53:fix/json-system-prompt-customization

Conversation

@Waqar53
Copy link
Contributor

@Waqar53 Waqar53 commented Jan 25, 2026

Add json_system_prompt parameter for Mode.JSON allowing users to customize
or disable the default system prompt.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)

Related Issues

Fixes #1514

Changes Made

  • Add json_system_prompt parameter to handle_json_modes()
  • Support {schema} placeholder for JSON schema injection
  • Empty string skips prompt modification entirely
  • 8 comprehensive unit tests

Note

Enables customizable system prompts for OpenAI JSON modes while preserving default behavior.

  • Adds json_system_prompt support in handle_json_modes for Mode.JSON, Mode.MD_JSON, and Mode.JSON_SCHEMA; supports {schema} substitution and empty-string to disable system prompt changes
  • Updates dispatcher in response.py to pass json_system_prompt through mode handlers
  • Keeps default "genius expert" prompt when unset; avoids modifying system message for JSON_SCHEMA
  • Adds comprehensive tests covering custom prompt, placeholder substitution, skipping, existing system message append, and mode-specific behavior

Written by Cursor Bugbot for commit 2bfb3ca. Configure here.

Waqar53 and others added 3 commits January 25, 2026 23:41
Fixes 567-labs#1932

Move pre-commit and ty from production dependencies to dev optional-dependencies.
These are development tools that should not be installed for production use.

Changes:
- Remove pre-commit>=4.3.0 from [project.dependencies]
- Remove ty>=0.0.1a23 from [project.dependencies]
- Add ty>=0.0.1a23 to [project.optional-dependencies.dev]
  (pre-commit was already present in dev)
Fixes 567-labs#1514

Add json_system_prompt parameter to handle_json_modes() allowing users to:
- Customize the system prompt with {schema} placeholder
- Set to empty string to skip prompt modification entirely
- Default behavior unchanged (backward compatible)

This enables use cases like roleplay where the 'genius expert' default
breaks character consistency.

Files:
- instructor/providers/openai/utils.py - Core implementation
- instructor/processing/response.py - Pass through parameter
- tests/test_json_system_prompt.py - 8 comprehensive tests
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

message = None
elif json_system_prompt is not None:
# Custom prompt provided - substitute {schema} placeholder
message = json_system_prompt.format(schema=json_schema)
Copy link

Choose a reason for hiding this comment

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

Format string crashes on prompts with curly braces

Medium Severity

Using str.format() for placeholder substitution causes a KeyError if json_system_prompt contains curly braces other than {schema}. Users may reasonably want to include JSON examples in their prompt like "Return JSON like: {"id": 1} matching {schema}", which will crash. Literal braces require escaping as {{ and }}, but this limitation isn't documented. Consider using a safer substitution method like str.replace("{schema}", json_schema) instead.

Fix in Cursor Fix in Web

Fixes Cursor Bugbot issue: str.format() crashes when prompts contain
curly braces like JSON examples (e.g., {"id": 1}). Using str.replace()
safely handles any curly braces in the custom prompt.
@Waqar53
Copy link
Contributor Author

Waqar53 commented Jan 26, 2026 via email

@jianxux
Copy link

jianxux commented Feb 2, 2026

🔧 Fix for Ruff lint failure:

Remove the unused imports in tests/test_json_system_prompt.py:

- import json
...
- from unittest.mock import MagicMock
+ from unittest.mock import patch

(Keep only the imports that are actually used in the test file.)

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.

Problem with system prompt when using Mode.JSON

3 participants