Skip to content

OpenAI Assistant: Allow temperature to be 0#369

Merged
AkhileshNegi merged 1 commit into
mainfrom
hotfix/assistant_temperature
Sep 4, 2025
Merged

OpenAI Assistant: Allow temperature to be 0#369
AkhileshNegi merged 1 commit into
mainfrom
hotfix/assistant_temperature

Conversation

@avirajsingh7
Copy link
Copy Markdown
Collaborator

@avirajsingh7 avirajsingh7 commented Sep 4, 2025

Summary

Previously, the temperature field validation prevented setting temperature to 0, which is a valid value for openai assistant. The sync logic also incorrectly treated 0 as a falsy value and defaulted to 0.1.

Checklist

Before submitting a pull request, please ensure that you mark these task.

  • Ran fastapi run --reload app/main.py or docker compose up in the repository root and test.
  • If you've fixed a bug or added code that is tested and has test cases.

Notes

Please add here if any other information is required for the reviewer.

Summary by CodeRabbit

  • Bug Fixes
    • Setting assistant temperature to 0 is now fully supported and retained during creation and updates.
    • Explicit 0 values are no longer overridden by a default; a default is applied only when temperature is not provided.
    • Validation now accepts 0 (range: 0 to 2), preventing errors when submitting 0 via UI or API.
    • Improves consistency and predictability of assistant behavior at low/zero temperature settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Sep 4, 2025

Walkthrough

Adjusts assistant temperature handling: validation models now allow temperature >= 0 (previously > 0), and sync logic preserves explicit 0 by defaulting only when temperature is None. No other functional changes.

Changes

Cohort / File(s) Summary
Temperature handling in sync
backend/app/crud/assistants.py
In sync_assistant, temperature now defaults to 0 only when source temperature is None; preserves explicit 0. Removed a blank line near assistant_id (formatting only).
Validation schema adjustments
backend/app/models/assistants.py
Updated Pydantic Field constraints: temperature changed from gt=0 to ge=0 in AssistantCreate and AssistantUpdate, allowing 0 as valid. No other field changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Client
  participant CRUD as sync_assistant
  participant OA as OpenAIAssistant
  participant DB

  Client->>CRUD: Request sync
  CRUD->>OA: Fetch assistant data
  OA-->>CRUD: assistant.temperature (may be None or number)
  alt temperature is None
    note right of CRUD: Default to 0
    CRUD->>DB: Save temperature = 0
  else temperature provided (incl. 0)
    note right of CRUD: Preserve provided value
    CRUD->>DB: Save temperature = provided
  end
  DB-->>CRUD: Persisted assistant
  CRUD-->>Client: Sync result
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nibble code like clover new,
Zero’s welcome in the dew—
Thermals set with mindful care,
None becomes a gentle air.
Models nod: “Let zero stay,”
Hops of logic, clean and gray.
Merge, then bask in sunny array.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch hotfix/assistant_temperature

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

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

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit 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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AkhileshNegi AkhileshNegi changed the title Allow assistant temperature to be 0 OpenAI Assistant: Allow temperature to be 0 Sep 4, 2025
@AkhileshNegi AkhileshNegi merged commit f646e4a into main Sep 4, 2025
2 of 3 checks passed
@AkhileshNegi AkhileshNegi deleted the hotfix/assistant_temperature branch September 4, 2025 04:18
Copy link
Copy Markdown

@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: 0

🧹 Nitpick comments (6)
backend/app/models/assistants.py (3)

69-70: Clarify “inclusive” in description

Minor doc tweak to avoid ambiguity on bounds.

-        default=0.1, ge=0, le=2, description="Sampling temperature between 0 and 2"
+        default=0.1, ge=0, le=2, description="Sampling temperature between 0 and 2 (inclusive)"

94-95: Mirror the “inclusive” phrasing in update model

Keep descriptions consistent.

-        default=None, ge=0, le=2, description="Sampling temperature between 0 and 2"
+        default=None, ge=0, le=2, description="Sampling temperature between 0 and 2 (inclusive)"

69-70: Unify default temperature across codepaths
Default temperature is 0.1 in the Assistant model (backend/app/models/assistants.py), falls back to 0 in the CRUD layer (backend/app/crud/assistants.py), and defaults to 1e-6 in the Collections route (backend/app/api/routes/collections.py). Pick one canonical value (e.g. 0.1) and update all three to use it.

backend/app/crud/assistants.py (3)

150-152: Default fallback changed to 0; verify intent and align with models default (0.1)

If OpenAI returns None, this now stores 0, diverging from AssistantBase/AssistantCreate default 0.1. Recommend keeping a single default (likely 0.1) unless product has decided zero is the canonical fallback.

-        temperature=openai_assistant.temperature
-        if openai_assistant.temperature is not None
-        else 0,
+        temperature=openai_assistant.temperature
+        if openai_assistant.temperature is not None
+        else 0.1,

If you prefer 0 as the global default, change AssistantBase/AssistantCreate defaults and the API AssistantOptions default to 0 for consistency.


144-156: Guard against out-of-range temperature from OpenAI (optional)

OpenAI should return 0–2, but adding a lightweight check prevents bad data persisting through this bypass of the Pydantic constraints on create/update models.

     db_assistant = Assistant(
         assistant_id=openai_assistant.id,
         name=openai_assistant.name or openai_assistant.id,
         instructions=openai_assistant.instructions,
         model=openai_assistant.model,
         vector_store_ids=vector_store_ids,
-        temperature=openai_assistant.temperature
-        if openai_assistant.temperature is not None
-        else 0,
+        temperature=(
+            openai_assistant.temperature
+            if (openai_assistant.temperature is not None)
+            else 0.1
+        ),
         max_num_results=max_num_results,
         project_id=project_id,
         organization_id=organization_id,
     )
+    # Optional: enforce range if upstream ever drifts
+    if not (0 <= db_assistant.temperature <= 2):
+        raise HTTPException(status_code=400, detail="Temperature must be between 0 and 2.")

150-152: Add tests for explicit 0 and None fallback

Ensure: (1) sync preserves 0 exactly; (2) None falls back to the chosen default; (3) out-of-range raises 400 if you add the guard.

I can add parametrized tests for these cases if you want a quick follow-up commit.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 21dc457 and 73c110e.

📒 Files selected for processing (2)
  • backend/app/crud/assistants.py (1 hunks)
  • backend/app/models/assistants.py (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
backend/app/models/assistants.py (1)
backend/app/api/routes/collections.py (1)
  • AssistantOptions (108-131)
backend/app/crud/assistants.py (2)
backend/app/tests/crud/test_assistants.py (1)
  • test_sync_assistant_success (27-47)
backend/app/api/routes/collections.py (1)
  • AssistantOptions (108-131)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: checks (3.11.7, 6)
🔇 Additional comments (3)
backend/app/models/assistants.py (2)

69-70: Allowing temperature=0 in create: LGTM

Changing gt>0 to ge>=0 correctly permits zero. No functional concerns.


94-95: Allowing temperature=0 in update: LGTM

Update model now accepts zero; matches the PR intent.

backend/app/crud/assistants.py (1)

150-152: Preserves explicit temperature=0: LGTM

Switching from a truthy check to an explicit None-check fixes the bug where 0 was overwritten.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants