Skip to content

Fix/incorrect json formatting#9

Merged
fqueis merged 4 commits intomainfrom
fix/incorrect-json-formatting
Mar 21, 2025
Merged

Fix/incorrect json formatting#9
fqueis merged 4 commits intomainfrom
fix/incorrect-json-formatting

Conversation

@fqueis
Copy link
Copy Markdown
Owner

@fqueis fqueis commented Mar 21, 2025

Description

This PR implements comprehensive JSON response handling and enhances error management across all service methods. Key improvements:

  • ✅ Proper JSON parsing when jsonMode=true
  • 🛡️ Type-safe generic return types for JSON responses
  • 🚨 Specific error handling for JSON parsing failures
  • 🔧 URL encoding fixes for GET parameters
  • 🧪 100% test coverage for new features

Changes

  1. JSON Response Handling

    • Added handleResponse utility for consistent JSON parsing
    • Updated all service methods to support generic return types
    async getGenerate<T>(prompt: string, params?: TextGenerationGetParams): Promise<string | T> {
      // ... implementation ...
    }
  2. Testing

    • Added 25 new test cases covering:
      • Valid/invalid JSON responses
      • Type safety verification
      • Error message formatting
      • Parameter encoding validation

Verification

  • All existing tests pass
  • New tests cover 100% of JSON handling paths
  • Manual verification with API endpoints shows:
    • Proper JSON objects returned when jsonMode=true
    • Raw strings returned when jsonMode=false
    • Correct error messages for parsing failures

Closes #8

fqueis added 4 commits March 21, 2025 16:49
…N responses

- Updated getGenerate, postGenerate, and vision methods to return either a string or a JSON object based on the jsonMode parameter.
- Introduced parseJson and sanitizeJson private methods for handling JSON parsing and sanitization.
- Improved type safety by allowing generic return types for methods in the TextService interface.
- Implemented tests for getGenerate, postGenerate, and vision methods to validate JSON response parsing and sanitization.
- Added scenarios for handling malformed JSON and already parsed JSON objects.
- Ensured error handling for invalid JSON responses after sanitization.
…just request builder

- Removed the prompt property from TextGenerationGetParams interface for cleaner structure.
- Updated TextGenerationGetRequestBuilder to include prompt as part of the generic type, enhancing type safety and clarity in request handling.
- Adjusted test cases to reflect the changes in the request parameters.
@fqueis fqueis added bug Something isn't working enhancement New feature or request labels Mar 21, 2025
@fqueis fqueis self-assigned this Mar 21, 2025
@fqueis fqueis merged commit 2fffc01 into main Mar 21, 2025
1 check passed
@fqueis fqueis deleted the fix/incorrect-json-formatting branch March 21, 2025 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proper JSON Response Handling Implementation

1 participant