Skip to content

Conversation

@fede-kamel
Copy link
Contributor

Summary

Implements response_format parameter to enable JSON mode and JSON schema output formatting for both Meta Llama and Cohere models. This allows users to leverage structured output capabilities via with_structured_output() and direct response_format configuration.

Changes

  • Add response_format field to OCIGenAIBase with comprehensive documentation
  • Implement response_format handling in ChatOCIGenAI._prepare_request method
  • Support response_format via class initialization, bind(), and model_kwargs
  • Add comprehensive unit tests covering all configuration methods
  • Add integration tests validating end-to-end functionality with real API calls

Implementation Details

The implementation ensures response_format is properly passed to both GenericChatRequest (Meta Llama) and CohereChatRequest (Cohere) models by adding the parameter to the model_kwargs in the _prepare_request method.

Testing

  • Unit Tests: 9/9 tests passing

    • Class-level response_format
    • Response_format via bind()
    • Response_format via model_kwargs
    • Pass-through to API for Generic and Cohere models
    • with_structured_output integration (json_mode and json_schema)
  • Integration Tests: 10/10 tests passing with real OCI API calls

    • JSON mode with Meta Llama and Cohere
    • Structured output with Pydantic models
    • JSON schema mode with Meta Llama
    • Complex nested structures

Fixes

Fixes #33

Breaking Changes

None - this is a new feature that is fully backward compatible.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 31, 2025
@fede-kamel
Copy link
Contributor Author

Hi @YouNeedCryDear! 👋

I hope you're doing well! I wanted to gently follow up on this PR that adds response_format parameter support for structured output.

Why this matters:
This implements a feature requested in issue #33 and enables users to leverage JSON mode and JSON schema output formatting for both Meta Llama and Cohere models through LangChain's standard with_structured_output() method.

What's been validated:

  • ✅ All 9 unit tests passing
  • ✅ All 10 integration tests passing with live OCI API calls
  • ✅ Fully backward compatible (no breaking changes)
  • ✅ Comprehensive documentation and examples included

This follows the same parameter pattern as other recent enhancements and should be a straightforward addition to the SDK's capabilities.

Would you have a chance to take a look when you get a moment? I'm happy to address any feedback or questions you might have!

Thanks so much for maintaining this project! 🙏

@YouNeedCryDear
Copy link
Member

@fede-kamel Why do we need response_format in the class? Can it be inside the model_kwargs? I don't see other vendors doing this.

@fede-kamel
Copy link
Contributor Author

fede-kamel commented Nov 15, 2025

Response to Review Feedback

@YouNeedCryDear
Thank you for the feedback! You're absolutely right that having response_format as a class field is inconsistent with other LangChain providers. I've addressed this concern by removing the class field and ensuring the functionality works through model_kwargs like other providers.

Changes Made

  1. Removed response_format class field from OCIGenAIBase - no longer needed as a class parameter
  2. Removed special handling in ChatOCIGenAI._prepare_request() that checked for the class field
  3. Updated all tests to use model_kwargs={"response_format": {...}} instead of class initialization
  4. Maintained full functionality - users can still set response format, just via model_kwargs

Why This Approach

This makes the OCI implementation consistent with other LangChain providers (OpenAI, Anthropic, etc.) where response format parameters are handled through model_kwargs rather than dedicated class fields. Users now have the same interface pattern across all providers.

Real-World Testing

To verify the functionality still works correctly after these changes, I tested with real OCI Generative AI API calls:

With Meta Llama 4 model:

llm = ChatOCIGenAI(
    model_id="meta.llama-4-maverick-17b-128e-instruct-fp8",
    model_kwargs={"response_format": {"type": "JSON_OBJECT"}},
    # ... other config
)

response = llm.invoke("Create JSON with name and age for Alice who is 30")
# Returns: {"name": "Alice", "age": 30}

Tested successfully with:

  • ✅ xAI Grok models
  • ✅ Meta Llama 3.3 models
  • ✅ Meta Llama 4 models

The response format functionality works perfectly and enforces proper JSON output from the models.

This change maintains backward compatibility while providing a cleaner, more consistent API design. Users can still access all the structured output features through with_structured_output() and direct model_kwargs configuration.

@fede-kamel
Copy link
Contributor Author

Hi @YouNeedCryDear, just following up—this PR is ready for another look whenever you have a moment. Appreciate your previous feedback!

@YouNeedCryDear
Copy link
Member

@fede-kamel Sorry I don't see any new changes after our discussion.

Implements response_format parameter to enable JSON mode and JSON schema
output formatting for both Meta Llama and Cohere models. This allows
users to leverage structured output capabilities via with_structured_output()
and direct response_format configuration.

Changes:
- Add response_format field to OCIGenAIBase with comprehensive documentation
- Implement response_format handling in ChatOCIGenAI._prepare_request method
- Support response_format via class initialization, bind(), and model_kwargs
- Add comprehensive unit tests covering all configuration methods
- Add integration tests validating end-to-end functionality with real API calls

The implementation ensures response_format is properly passed to both
GenericChatRequest (Meta Llama) and CohereChatRequest (Cohere) models.

Fixes oracle#33
- Remove response_format field from OCIGenAIBase class
- Remove special handling in ChatOCIGenAI._prepare_request()
- Update tests to use model_kwargs for response_format
- Maintain functionality while ensuring consistency with other LangChain providers

This addresses the reviewer's concern about having response_format as a class field instead of handling it through model_kwargs like other providers.
@fede-kamel fede-kamel force-pushed the feature/response-format-support branch from 299d2ca to 7c08d7c Compare November 18, 2025 13:37
@fede-kamel
Copy link
Contributor Author

Hi @YouNeedCryDear! 👋

The changes have now been pushed to the PR. I apologize for the confusion - the commit addressing your feedback was created locally but wasn't pushed to GitHub until now.

Changes Applied:

  • ✅ Removed response_format class field from OCIGenAIBase
  • ✅ Removed special handling in ChatOCIGenAI._prepare_request()
  • ✅ Updated all tests to use model_kwargs={"response_format": ...} instead
  • ✅ Rebased on latest main branch

The functionality remains the same, but now follows the same pattern as other LangChain providers by handling response_format through model_kwargs rather than as a dedicated class field.

You should now see two commits in the PR:

  1. The original feature implementation
  2. The changes addressing your feedback

Ready for another review when you have a chance. Thanks for your patience!

- Fix line length violations by splitting long strings
- Auto-fix import ordering and remove unused imports
- Run ruff format on all modified files
- All ruff and mypy checks now pass
@fede-kamel
Copy link
Contributor Author

fede-kamel commented Nov 18, 2025

The CI failures in libs/oracledb are due to GitHub runner disk space issues ("No space left on device"), not code issues.

All libs/oci checks pass successfully:

The oracledb failures are unrelated to this PR (no oracledb files were modified). Could you re-run the failed checks when you get a chance? Thanks!

@YouNeedCryDear

@YouNeedCryDear YouNeedCryDear self-requested a review November 20, 2025 21:52
@YouNeedCryDear YouNeedCryDear merged commit cc2668e into oracle:main Nov 20, 2025
5 of 10 checks passed
@fede-kamel fede-kamel deleted the feature/response-format-support branch November 21, 2025 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unrecognized keyword arguments: response_format

2 participants