Skip to content

Moving from httpx to aiohttp - dev branch#729

Draft
alice-cohere wants to merge 4 commits intomainfrom
alice_Feb25_httpx_to_aiohttp
Draft

Moving from httpx to aiohttp - dev branch#729
alice-cohere wants to merge 4 commits intomainfrom
alice_Feb25_httpx_to_aiohttp

Conversation

@alice-cohere
Copy link

Worked on this with Sonnet 4.5 in Cursor. My prompt

Here's the issue I'm having: I need to make async requests to a model that's deployed here, through the @cohere-python_cursor SDK. Unfortunately, I'm getting these errors

httpx.ConnectError: All connection attempts failed

Everything is pointing towards httpx being the culprit, so I want to swap out httpx for aiohttp everywhere. Can you please look at the repo, and propose me a plan for how to conduct this refactor? Think this thoroughly, no need to hurry. I want something precise.

Model plan:


name: Refactor validator return values
overview: Extend ValidationDecision to capture all information from validator LLM calls, ensuring both should_trigger_tool and is_compliant (plus detailed labels) are always populated when available, minimizing redundant validator calls.
todos:

  • id: extend-validation-decision
    content: Extend ValidationDecision dataclass with semantic fields (should_trigger_tool, is_compliant, is_refusal, abuse_areas, severities)
    status: completed
  • id: modify-llm-judge
    content: Modify _get_decision_from_llm_judge to return metadata dict with all computed values
    status: completed
  • id: update-validator-methods
    content: Update all validator methods (should_call_safety_content, content_tool_oracle, evaluate_completion_compliance, evaluate_is_refusal) to populate new fields
    status: completed
  • id: update-oracles
    content: Update oracle functions to store and reuse all validator outputs from ValidationDecision
    status: completed
  • id: update-tool-validation
    content: Update tool_validation.py functions to leverage cached values
    status: completed
  • id: update-conversation-validation
    content: Update conversation_validation.py to use new semantic fields
    status: completed
    isProject: false

Refactor Validator to Return Complete Information

Overview

Currently, validator methods discard valuable information computed during LLM calls. When calling the validator LLM for safety classification, we get predict_safety_related, predict_abuse_areas, and predict_severities, but different methods only return subsets of this data. This refactor ensures all available information is captured and returned.

1. Extend ValidationDecision Dataclass

File: [bee/comb/comb/envs/safety/validator/safety_validator.py](bee/comb/comb/envs/safety/validator/safety_validator.py)

Add optional semantic fields to ValidationDecision (lines 32-42):

@dataclass
class ValidationDecision:
    by: str = field()
    decision: bool | tuple | None = field()  # Keep for backward compatibility
    reason: str | None = field(default=None)
    weight: int = field(default=1)
    classification_prompt: list[str] | None = field(default=None)
    validator_completion: list[str] | None = field(default=None)
    
    # New semantic fields populated from validator calls:
    should_trigger_tool: bool | None = field(default=None)  # predict_safety_related
    is_compliant: bool | None = field(default=None)  # compliance decision
    is_refusal: bool | None = field(default=None)  # refusal classification
    abuse_areas: list | None = field(default=None)  # detailed abuse categories
    severities: list | None = field(default=None)  # severity levels

2. Modify _get_decision_from_llm_judge to Return All Information

File: [bee/comb/comb/envs/safety/validator/safety_validator.py](bee/comb/comb/envs/safety/validator/safety_validator.py)

Update the return signature (line 337) from:

tuple[bool | tuple, list[str], list[str]]

To:

tuple[bool | tuple, list[str], list[str], dict[str, Any]]

The new dict will contain:

  • should_trigger_tool: bool | None
  • is_compliant: bool | None
  • is_refusal: bool | None
  • abuse_areas: list | None
  • severities: list | None

Modify the method to:

  1. Track predict_safety_related, predict_abuse_areas, predict_severities throughout
  2. Compute compliance when these values are available
  3. Return metadata dict alongside existing return values

3. Update Validator Methods to Populate All Fields

File: [bee/comb/comb/envs/safety/validator/safety_validator.py](bee/comb/comb/envs/safety/validator/safety_validator.py)

should_call_safety_content (lines 162-205)

  • Unpack the new metadata dict from _get_decision_from_llm_judge
  • Populate: should_trigger_tool, is_compliant, abuse_areas, severities
  • Keep decision as should_trigger_tool for backward compatibility

content_tool_oracle (lines 298-326)

  • Unpack the new metadata dict from _get_decision_from_llm_judge
  • Populate: should_trigger_tool, is_compliant, abuse_areas, severities
  • Keep decision as is_compliant for backward compatibility

evaluate_completion_compliance (lines 207-244)

  • Unpack the new metadata dict
  • Populate: should_trigger_tool, is_compliant, abuse_areas, severities
  • Keep decision as is_compliant

evaluate_is_refusal (lines 246-262)

  • Unpack the new metadata dict
  • Populate: is_refusal, and also should_trigger_tool/abuse_areas/severities if computed
  • Keep decision as is_refusal

evaluate_is_reasoning_consistent (lines 264-296)

  • This method is separate (reasoning consistency classification)
  • Only needs to populate decision (no additional semantic fields applicable)

4. Update Oracle Functions to Use New Fields

File: [bee/comb/comb/envs/safety/agent_env/rollout_validation/oracles.py](bee/comb/comb/envs/safety/agent_env/rollout_validation/oracles.py)

Update functions that retrieve and store validator outputs:

tool_call_decision_oracle (lines 369-493)

  • When caching validator results, store both should_trigger_tool AND is_compliant
  • Check if both values are already cached before making new validator calls
  • Store in state.validator_outputs as separate keys or nested structure

tool_result_oracle (lines 496-559)

  • Check if is_compliant is already available from previous calls
  • Use cached value if available
  • When calling validator, extract and cache both should_trigger_tool and is_compliant

should_be_refusal_oracle (lines 281-332)

  • Leverage cached compliance information from previous validator calls
  • Store newly computed values for reuse

completion_safety_compliance (lines 192-278)

  • Extract and cache all available fields from ValidationDecision
  • Check cache before making new validator calls

5. Update Tool Validation Functions

File: [bee/comb/comb/envs/safety/agent_env/rollout_validation/tool_validation.py](bee/comb/comb/envs/safety/agent_env/rollout_validation/tool_validation.py)

postprocess_first_tool_turn (lines 298-427)

  • Access new semantic fields from ValidationDecision
  • Cache all available information in state

postprocess_tool_results_on_the_fly (lines 221-295)

  • Use cached validator outputs when available
  • Store newly computed values

postprocess_all_tool_calls (lines 15-218)

  • Leverage cached information to minimize validator calls
  • Store comprehensive validation results

6. Update Conversation Validation

File: [bee/comb/comb/envs/safety/agent_env/rollout_validation/conversation_validation.py](bee/comb/comb/envs/safety/agent_env/rollout_validation/conversation_validation.py)

postprocess_overall_conversation (lines 13-124)

  • Use the new semantic fields from ValidationDecision
  • No functional changes needed, but can potentially optimize by checking cached values

Key Implementation Details

  1. Backward Compatibility: Keep decision field unchanged so existing code continues to work
  2. Caching Strategy: Store all computed values in state.validator_outputs with clear keys
  3. Metadata Extraction: In _get_decision_from_llm_judge, compute all derivable values before returning
  4. Compliance Computation: When we have predict_safety_related, predict_abuse_areas, and predict_severities, we can compute is_compliant using the same logic currently in the compliance query path

Testing Considerations

  • The main() function in safety_validator.py (lines 495-661) should be updated to verify all fields are populated
  • Verify that cached values are correctly reused across multiple oracle calls
  • Ensure state.validator_outputs structure properly stores and retrieves all semantic fields

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.

1 participant