Skip to content

enhancement: serialisation optimisations#194

Merged
akshaydeo merged 1 commit intomainfrom
07-29-enhancement_serialisation_optimisations
Jul 29, 2025
Merged

enhancement: serialisation optimisations#194
akshaydeo merged 1 commit intomainfrom
07-29-enhancement_serialisation_optimisations

Conversation

@Pratham-Mishra04
Copy link
Copy Markdown
Collaborator

No description provided.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 29, 2025

Summary by CodeRabbit

  • Refactor
    • Switched JSON serialization and deserialization across all providers and core modules to use a faster, more efficient library.
    • Unified error handling structures for more consistent and reliable error reporting.
    • Simplified and streamlined internal response handling for improved maintainability.

No changes to user-facing features or APIs.

Walkthrough

The changes replace all uses of the encoding/json and goccy/go-json libraries with the bytedance/sonic library for JSON serialization and deserialization across the codebase. Error and response handling is unified by adopting shared schema types, and some internal response pooling and error parsing logic is updated accordingly. No direct API or exported entity signatures are altered except for internal type substitutions.

Changes

Cohort / File(s) Change Summary
Dependency Updates
core/go.mod
Adds new indirect dependencies, including sonic, base64x, cpuid/v2, golang-asm, and golang.org/x/arch. No removals or version changes for existing modules.
OpenAI Provider Refactor
core/providers/openai.go
Replaces goccy/go-json with sonic for JSON operations. Removes local OpenAIError struct in favor of schemas.BifrostError. Updates response pooling to use schemas.BifrostResponse. Adjusts error and streaming handling.
Provider Utilities
core/providers/utils.go
Switches JSON unmarshaling from goccy/go-json to sonic. Removes concurrent raw response unmarshaling and simplifies return values in response handling.
Vertex Provider Error Handling
core/providers/vertex.go
Changes error response parsing to use schemas.BifrostError instead of a local type. Updates response Usage field assignment.
Schemas JSON Codec Swap
core/schemas/bifrost.go
Replaces all uses of encoding/json with sonic for custom marshal/unmarshal logic. Removes encoding/json import. No logic changes.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Provider (OpenAI/Vertex)
    participant Sonic (JSON)
    participant Schemas

    Client->>Provider (OpenAI/Vertex): Send request
    Provider (OpenAI/Vertex)->>Sonic (JSON): Marshal/Unmarshal request/response
    Sonic (JSON)-->>Provider (OpenAI/Vertex): Serialized/Deserialized data
    Provider (OpenAI/Vertex)->>Schemas: Use BifrostResponse/BifrostError
    Schemas-->>Provider (OpenAI/Vertex): Structured data
    Provider (OpenAI/Vertex)-->>Client: Return response or error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • danpiths
  • akshaydeo

Poem

In fields of code where JSON ran,
The Sonic hare now leads the clan.
Errors unified, responses pooled,
Old types retired, new rules are schooled.
With every hop, the schemas sing—
A faster, tidier marshaling!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 07-29-enhancement_serialisation_optimisations

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Collaborator Author

Pratham-Mishra04 commented Jul 29, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 29, 2025
coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 29, 2025
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🔭 Outside diff range comments (1)
core/providers/utils.go (1)

311-349: Simplify concurrent unmarshaling logic

The current implementation has unnecessary complexity with commented-out code and unused variables. The concurrent goroutine pattern is no longer needed since raw response unmarshaling has been removed.

Apply this diff to simplify the function:

 func handleProviderResponse[T any](responseBody []byte, response *T) (interface{}, *schemas.BifrostError) {
-	// var rawResponse interface{}
-
-	var wg sync.WaitGroup
-	var structuredErr, rawErr error
-
-	wg.Add(2)
-	go func() {
-		defer wg.Done()
-		structuredErr = sonic.Unmarshal(responseBody, response)
-	}()
-	go func() {
-		defer wg.Done()
-		// rawErr = sonic.Unmarshal(responseBody, &rawResponse)
-	}()
-	wg.Wait()
-
-	if structuredErr != nil {
+	if err := sonic.Unmarshal(responseBody, response); err != nil {
 		return nil, &schemas.BifrostError{
 			IsBifrostError: true,
 			Error: schemas.ErrorField{
 				Message: schemas.ErrProviderDecodeStructured,
-				Error:   structuredErr,
+				Error:   err,
 			},
 		}
 	}
 
-	if rawErr != nil {
-		return nil, &schemas.BifrostError{
-			IsBifrostError: true,
-			Error: schemas.ErrorField{
-				Message: schemas.ErrProviderDecodeRaw,
-				Error:   rawErr,
-			},
-		}
-	}
-
 	return nil, nil
 }

Comment thread core/providers/vertex.go
Copy link
Copy Markdown
Contributor

akshaydeo commented Jul 29, 2025

Merge activity

  • Jul 29, 5:14 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jul 29, 5:14 PM UTC: @akshaydeo merged this pull request with Graphite.

@akshaydeo akshaydeo merged commit 53cfc26 into main Jul 29, 2025
2 checks passed
@akshaydeo akshaydeo deleted the 07-29-enhancement_serialisation_optimisations branch August 31, 2025 17:28
akshaydeo pushed a commit that referenced this pull request Nov 17, 2025
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.

2 participants