Skip to content

fix(agent): extract JSON from a bare fence, not only json - #680

Open
PratikDhanave wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
PratikDhanave:fix/extract-json-bare-fence
Open

fix(agent): extract JSON from a bare fence, not only json#680
PratikDhanave wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
PratikDhanave:fix/extract-json-bare-fence

Conversation

@PratikDhanave

Copy link
Copy Markdown

What

Makes the tool-use-shim response parser tolerant of a bare ``` code fence, not only ```json. Adds unit tests for the (previously untested) extractJSON and parseReActResponse helpers.

Why

For models without native function-calling, kubectl-ai uses a ReAct-style shim and parses the model's JSON out of a fenced code block via extractJSON:

const jsonBlockMarker = "```json"
first := strings.Index(s, jsonBlockMarker)

It only recognized ```json. But models frequently emit the JSON in a bare ``` block (no language tag). In that case extractJSON returned false, parseReActResponse errored, and the whole turn failed — even though the model produced a perfectly valid action.

Change

extractJSON now falls back to a bare ``` fence when ```json is absent. The ```json path is unchanged (byte-for-byte), so existing behavior is preserved; this only adds a fallback.

Testing

Adds TestExtractJSON and TestParseReActResponse (these helpers had no tests) covering the json fence, the new bare fence, and no-fence / invalid-JSON inputs. go test ./pkg/agent/ and go vet pass.

parseReActResponse (used by the tool-use shim for models without native
function calling) relies on extractJSON, which only recognized a ```json fence.
Models frequently emit the JSON in a bare ``` block with no language tag, in
which case extraction returned false and the whole turn failed to parse.

Fall back to a bare ``` fence when ```json is absent. The ```json path is
unchanged. Adds table tests for extractJSON and parseReActResponse (previously
untested), covering the ```json fence, the bare ``` fence, and no-fence/invalid
inputs.
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