Skip to content

Conversation

@ejunker
Copy link
Contributor

@ejunker ejunker commented May 7, 2025

Description

The buildPayload() methods are using array_filter() without a callback which will remove any falsey values such as the literal 0 value. This is a problem if you want to set your temperature to 0 or your topP to 0 because those values will be filtered out. The code was updated to provide a callback that checks for null values explicitly.

…ack to filter out null values but keep 0 values
@ejunker ejunker force-pushed the allow-zero-vals branch from f755300 to bd70048 Compare May 7, 2025 21:36
@ejunker
Copy link
Contributor Author

ejunker commented May 8, 2025

I just noticed this same problem also exists in the main prism-php/prism repo.

And Laravel has a Arr::whereNotNull($array); helper if you want to use that instead of the array_filter()

@ChrisB-TL
Copy link
Collaborator

Good catch! Would you mind adding an HTTP test to make sure a zero value is sent, to prevent future regressions?

See example on another PR here: 8abecee

@ChrisB-TL ChrisB-TL self-assigned this May 12, 2025
@ejunker
Copy link
Contributor Author

ejunker commented May 13, 2025

@ChrisB-TL Would a test like this be suitable? I tried using Prism::fake() but then the buildPayload() method is not called.

it('removes null values from payload', function (): void {
    FixtureResponse::fakeResponseSequence('converse', 'converse/structured');

    $schema = new ObjectSchema(
        'output',
        'the output object',
        [
            new StringSchema('weather', 'The weather forecast'),
            new StringSchema('game_time', 'The tigers game time'),
            new BooleanSchema('coat_required', 'whether a coat is required'),
        ],
        ['weather', 'game_time', 'coat_required']
    );

    Prism::structured()
        ->withSchema($schema)
        ->using('bedrock', 'anthropic.claude-3-5-haiku-20241022-v1:0')
        ->withProviderOptions([
            'apiSchema' => BedrockSchema::Converse,
            'guardRailConfig' => null,
        ])
        ->withMaxTokens(2048)
        ->usingTemperature(0)
        ->asStructured();

    Http::assertSent(function (Request $request) {
        return expect($request->data())->toMatchArray([
            'inferenceConfig' => [
                'maxTokens' => 2048,
                'temperature' => 0,
            ],
        ])->not()->toHaveKey('guardRailConfig');
    });
});

@sixlive sixlive requested a review from ChrisB-TL May 16, 2025 14:36
@sixlive sixlive merged commit c03ca7f into prism-php:main May 28, 2025
14 checks passed
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.

3 participants